springboot单元测试

1.除了默认的spring-boot-starter-test依赖外还需要引入junit依赖

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

2.springboot默认测试类如下

默认测试类执行会加载spring上下文,且该@Test注解不能再写其它测试类,所以需要将默认@Test注解改成import org.junit.Test;包下的@Test注解,并且类和方法都需要用public修饰,junit的注解可以写多个测试类。

3.@RunWith(SpringRunner.class)注解在junit包下,所以只有加了junit依赖才有效,使用该注解会加载spring上下文,不使用则不加载

原文地址:https://www.cnblogs.com/coderxiaobai/p/13705453.html