Junit5单元测试
引入依赖
Junit中文官方文档戳这里→
Junit中文官方文档
<!-- Junit5对应的组件-->
<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注解
@DisplayName("Junit5功能测试")
public class TestJunit5 {
@DisplayName("DisplayName")
@Test
void testDisplayName(){
System.out.println("测试testDisplayName注解");
}
}
运行效果如下图
BeforeEach和AfterEach注解
@BeforeEach
void testBeforeEach(){
System.out.println("每个测试方法执行前执行...");
}
@AfterEach
void testAfterEach(){
System.out.println("每个测试方法执行后执行...");
}
运行效果如下图
BeforeAll和AfterAll注解
@DisplayName("Junit5功能测试")
public class TestJunit5 {
@DisplayName("DisplayName")
@Test
void testDisplayName(){
System.out.println("测试testDisplayName注解");
}
@DisplayName("单纯测试案例")
@Test
void test(){
System.out.println("我是单纯的测试案例");
}
@BeforeAll
static void testBeforeAll(){
System.out.println("所有测试案例开始前运行");
}
@AfterAll
static void testAfterAll(){
System.out.println("所有测试案例结束后运行");
}
}
运行效果如下图
BeforeAll和AfterAll注解
@ParameterizedTest
@ValueSource(strings = { "racecar", "radar", "able was I ere I saw elba" })
void palindromes(String candidate) {
assertTrue(isPalindrome(candidate));
}
private boolean isPalindrome(String candidate) {
System.out.println(candidate);
return true;
}
运行结果如下图
Disabled 注解
@Disabled //禁用注解
@DisplayName("测试方法2")
@Test
void testDisabled() {
System.out.println("@Disabled注解禁用");
}
运行结果如下图
RepeatedTest 注解
@RepeatedTest(5) //parameter指的是重复次数
@Test
void testRepeatedTest() {
System.out.println("重复执行该方法");
}
运行结果如下图
assertEquals 注解
@DisplayName("standardAssertions测试")
@Test
void standardAssertions() {
assertEquals(4, 4, "expected == actual 时该message内容不显示");
assertEquals(2, 4, "expected != actual 时该message内容显示");
}
运行结果如下图
分组+依赖断言
@Test
void dependentAssertions() {
Book book = new Book(null,"Jane","story");
// Within a code block, if an assertion fails the
// subsequent code in the same block will be skipped.
assertAll("properties",
() -> {
String bookname = book.getBookname();
assertNotNull(bookname);
// Executed only if the previous assertion is valid.
assertAll("book name",
() -> assertTrue(bookname.startsWith("G")),
() -> assertTrue(bookname.endsWith("u"))
);
},
() -> {
String author = book.getAuthor();
assertNotNull(author);
assertAll("author",
() -> assertTrue(author.startsWith("J")),
() -> assertTrue(author.endsWith("e"))
);
},
() -> {
String kind = book.getKind();
assertNotNull(kind);
assertAll("kind",
() -> assertTrue(kind.startsWith("t"),"your expected start value is wrong"),
() -> assertTrue(kind.endsWith("u"),"your expected end value is wrong")
);
}
);
}
运行结果如下图
异常断言
@Test
void exceptionTesting() {
Throwable exception = assertThrows(ArithmeticException.class, () -> {
int a = 8 / 0;
System.out.println(a);
throw new IllegalArgumentException("a message");
});
assertEquals("a message", exception.getMessage());
}
@Test
void timeoutNotExceeded() {
// The following assertion succeeds.
assertTimeout(ofMillis(2), () -> {
// Perform task that takes less than 2 minutes.
Thread.sleep(3);
});
}
运行结果如下图
假设测试
@Test
void testOnlyOnCiServer() {
//assertTrue不影响这句话的执行
System.out.println("前面...");
assertTrue(8>5);
//assertTrue影响这句话的执行
System.out.println("后面...");
}
运行结果如下图