@runWith注解作用:
--@RunWith就是一个运行器
--@RunWith(JUnit4.class)就是指用JUnit4来运行
--@RunWith(SpringJUnit4ClassRunner.class),让测试运行于Spring测试环 境,以便在测试开始的时候自动创建Spring的应用上下文
--@RunWith(Suite.class)的话就是一套测试集合

引申:
Spring Boot 1.5.2 Junit测试
使用 Spring 进行单元测试

方法1:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@EnableAutoConfiguration
public class BBTestAA {
@Autowired
private TestRestTemplate testRestTemplate; //Application.class 为SpringBoot的启动入口类,每个SpringBoot项目大家都会配置
}
如果pom.xml中有如下代码,这行@RunWith(SpringRunner.class)就不会出现SpringRunner,反而有@RunWith(SpringJUnit4ClassRunner.class)
<!--spring-test测试=-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
如果pom.xml中没有这段,则@RunWith(SpringRunner.class)不会报错。如果有这段:①未注释<scope>test</scope>会报错;②注释<scope>test</scope>不会报错
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
如果pom.xml中没有这段,则会报错。如果有这段:①未注释<scope>test</scope>SpringRunner、SpringBootTest无法引用,会报错;②注释<scope>test</scope>不会报错
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>1.5.9.RELEASE</version>
<scope>test</scope>
</dependency>

总结起来,想要使用

@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class)

pom.xml中应该引用这两个

<!--spring-test测试=-->
<!--<dependency>-->
<!--<groupId>org.springframework</groupId>-->
<!--<artifactId>spring-test</artifactId>-->
<!--<version>4.2.4.RELEASE</version>-->
<!--</dependency>--> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>1.5.9.RELEASE</version>
<!--<scope>test</scope>-->
</dependency> <!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<!--<scope>test</scope>-->
</dependency>

方法2:
如果有<scope>test</scope>@RunWith报红,没有<scope>test</scope>会引入该类

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

如果有<scope>test</scope>@SpringBootTest报红,没有<scope>test</scope>会引入该类

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>1.5.9.RELEASE</version>
<scope>test</scope>
</dependency>

如果是<version>4.2.4.RELEASE</version>SpringRunner报红,如果<version>4.2.4.RELEASE</version>会引入该类

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>

所以最后要正确使用,需引入这些架包

  <!--spring-test测试=-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.7.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>1.5.9.RELEASE</version>
</dependency> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>

2.在IDE中新增JunitTest类

@RunWith(SpringRunner.class) //14.版本之前用的是SpringJUnit4ClassRunner.class
@SpringBootTest(classes = Application.class) //1.4版本之前用的是//@SpringApplicationConfiguration(classes = Application.class)
public class SystemInfoServiceImplTest { @Autowired
private ISystemInfoService systemInfoservice; @Test
public void add() throws Exception {
} @Test
public void findAll() throws Exception {
} }

主要是注解的更改,如果注解用的不对,会报各种奇怪的问题,例如applicationContext找不到,datasource实例化失败等等。

  1. 为了支持上面两个注解,maven文件中要用对依赖以及版本,我当时添加SpringRunner.class所在的依赖jar时,由于用了idea的auto-imported,IDE自动导入了版本是3.x的,实际应该导入4.x,我一直以为idea导入的是正确的,导致在这上面费时颇多,后来我手工写入就解决了。下面是正确的spring boot test的maven依赖

Springboot测试类之@RunWith注解的更多相关文章

  1. Springboot 测试类没有找到bean注入

    其他乱七八糟配置就不扯了,先上项目结构图 配置好参数后我再src/test/java类测试访问数据库时发现bean没有正确的注入.值得注意的是,这个项目的启动类是叫App.java 所以我们必须在这个 ...

  2. SpringBoot测试类启动错误 java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

    报错 java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @Cont ...

  3. JPA实体类注解、springboot测试类、lombok的使用

    前提准备: 搭建一个springboot项目,详情请参见其它博客:点击前往 1 引入相关依赖 web.mysql.jpa.lombok <?xml version="1.0" ...

  4. IntelliJ IDEA 2017版 SpringBoot测试类编写

    SpringBoot的测试类编写Demo 源码见 https://github.com/liushaoye/baseone.git

  5. springboot测试类

    Controller测试类 /** * Created by zhiqi.shao on 2017/5/12. */ @RunWith(SpringJUnit4ClassRunner.class) @ ...

  6. 15.junit测试类使用及注解

    1.junit简介 JUnit是一个Java语言的单元测试框架,可以大大缩短你的测试时间和准确度.多数Java的开发环境都已经集成了JUnit作为单元测试的工具. 2.实现junitDemo示例 2. ...

  7. springboot 测试类,项目使用shiro时报错UnavailableSecurityManagerException

    大概的问题就是,正常运行项目是没有问题的 使用测试类是,加载不了shiro的securityManager,主要导致不是很清楚,望告知, 解决方法 @Resource org.apache.shiro ...

  8. SpringBoot 测试类 @RunWith & @SpringBootTest

    @RunWith(SpringRunner.class) @SpringBootTest public class RabbitMqTest { @Autowired RabbitMqSender r ...

  9. 详谈springboot启动类的@SpringBootApplication注解

    前几天我们学会了如何创建springboot项目今天我们说一下他是怎么运行的为什么不需要我们再去编写繁重的配置文件的 @SpringBootApplication 首先我们看一下这个注解,他是用来标注 ...

随机推荐

  1. vue_02day

    目录 vue_02 表单指令: 条件指令: 循环指令: 前端数据库: 分隔符: 过滤器: 计算属性: 监听属性: vue编译不生效,闪烁 冒泡排序: vue_02 表单指令: <form act ...

  2. [LeetCode] 159. Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串

    Given a string s , find the length of the longest substring t  that contains at most 2 distinct char ...

  3. 命令行编译带外部包依赖的java源文件 [以JDBC MySQL8为例]

    环境: MySQL8 JDK11(SE) 首先下载MySQL8的JDBC驱动 https://dev.mysql.com/downloads/connector/j/选 PlatForm Indepe ...

  4. [转]VS2017避免全部安装至C盘

    VS2017避免全部安装至C盘 版权声明:商业用途请联系博主,非商业转载请标明出处. https://blog.csdn.net/qq_15807167/article/details/7247208 ...

  5. HOT SUMMER 每天都是不一样,积极的去感受生活 C#关闭IE相应的窗口 .

    window.close(); System.Diagnostics.Process[]   myProcesses;     myProcesses   =   System.Diagnostics ...

  6. Mysql select into outfile 命令

    [1]Mysql select into outfile命令 在Mysql中,与load data infile命令作用相反的一个命令是select into outfile命令 select int ...

  7. logstash 对配置文件conf敏感信息,密码等加密

    logstash的配置文件conf经常会涉及敏感信息,比如ES,mysql的账户密码等,以下使用logstash导入mysql为例子,加密隐藏mysql的密码. 在向keystore中添加key及其s ...

  8. 排序算法Java代码实现(五)—— 快速排序

    本篇内容: 快速排序 快速排序 算法思想: 通过一趟排序将要排序的数据分割成独立的两部分, 其中一部分的所有数据都比另外一部分的所有数据都要小, 然后再按此方法对这两部分数据分别进行快速排序, 整个排 ...

  9. 在<a></a>标签中如何调用javaScript脚本

    在日常工作总会遇到在<a>标签中执行js代码的情况 现在做一个总结,希望对大家有一个帮助. 1.a href="javascript:js_method();" 这是我 ...

  10. C#使用Redis实现网站统计访问数或点赞数功能!

    1.安装.net操作Redis需要的NuGet包: 这里推荐使用:StackExchange.Redis,在程序包管理器控制台输入命令install-package stackexchange.red ...