导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
或
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
常用注解:
1)@Test
:标识这是一个测试方法,可以直接运行
2)@RepeatedTest
:标识这是一个可重复执行多次的测试方法,可直接运行
注意:如果一个测试用法同时标注了@Test、@RepeatedTest注解,执行次数会叠加,即@RepeatedTest指定了测试次数+1
3)Disabled
:表示测试方法默认关闭,即不能直接执行
4)DisplayName
:为测试类或测试方法设置展示名称
5)@BeforeEach、@AfterEach
:在每一个测试方法之前、之后执行
6)@BeforeAll、@AfterAll
:在所有单元测试执行之前、之后执行
注意:需要是加了static修饰的静态方法才能有这样的功能
7)@Timeout
:设置方法执行的超时时间,如果超时则出错