Springboot测试类之@RunWith注解
- Springboot测试类之@RunWith注解

Springboot测试类之@RunWith注解
@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 {@Autowiredprivate 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>