[TOC]
Springboot低版本使用Junit5
排除低版本对junit的依赖
1
2
3
4
5
6
7
8
9
10
11
|
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
|
引入junit5
1
2
3
4
5
|
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.6.0</version>
</dependency>
|
JUnit 5 @BeforeAll和@AfterAll的用法
原文:https://majing.io/posts/10000009741162
JUnit 5的@BeforeAll和@AfterAll注释分为两种情景使用:静态方法和实例方法。
静态方法
@BeforeAll和@AfterAll默认是添加到静态方法上,@BeforeAll注释的静态方法会在执行所有测试用例之前调用,@AfterAll注释的静态方法会在所有的测试用例完成后调用。它们是在应用级别,应用于所有实例对象。
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Junit5Test {
private static final Logger LOGGER = LoggerFactory.getLogger(Junit5Test.class);
@BeforeAll
static void beforeAll() {
LOGGER.info("beforeAll called");
}
@Test
public void aTest1() {
LOGGER.info("aTest1 called");
LOGGER.info(this.toString());
}
@Test
public void aTest2() {
LOGGER.info("aTest2 called");
LOGGER.info(this.toString());
}
@AfterAll
static void afterAll() {
LOGGER.info("afterAll called");
}
}
|
实例方法
@BeforeAll注释的实例方法会在实例所有的测试用例之前调用,@AfterAll注释的实例方法会在所有实例的测试用例之后调用。但如果@BeforeAll或@AfterAll要应用在实例方法上,需要在实例的类上添加注释@TestInstance。
1
2
3
4
|
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class Junit5Test {
....
}
|