单元测试
导入依赖
1、导入场景启动器spring-boot-starter-test
2、SpringBoot 2.4 以上版本移除了默认对 Vintage 的依赖,如果需要兼容junit4需要自行引入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
兼容junit4,需导入下面依赖
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
常用注解
@DisplayName(表示测试类或测试方法不执行)、@Test(表示方法是测试方法)
import org.junit.jupiter.api.Test;
@DisplayName("junit5测试功能类")
public class Junit5Test {
@DisplayName("测试displayname注解")
@Test //注意这里使用的是jupiter的Test注解!!
void testDisPlayName() {
System.out.println("1");
}
}
@BeforeAll(表示在所有单元测试之前执行) 、@AfterAll(表示在所有单元测试之后执行)
@BeforeAll
static void testBeforeAll() {
System.out.println("所有单元测试就要开始了");
}
@AfterAll
static void testAfterAll() {
System.out.println("所有单元测试就要结束了。。。");
}