網上有若干的junit5的教程, 可惜好多的跑不起來, 所以決定自己寫一個, 作爲junit4的升級版本, 還是有很多的長進的.
項目的junit5的依賴是:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<junit.version>4.12</junit.version>
<junit.jupiter.version>5.5.2</junit.jupiter.version>
<junit.vintage.version>5.5.2</junit.vintage.version>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>${junit.vintage.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
- @BeforeAll 只執行一次,執行時機是在所有測試和 @BeforeEach 註解方法之前。
- @BeforeEach 在每個測試執行之前執行。
- @AfterEach 在每個測試執行之後執行。
- @AfterAll只執行一次,執行時機是在所有測試和 @AfterEach 註解方法之後。
因爲框架會爲每個測試創建一個單獨的實例,在 @BeforeAll/@AfterAll 方法執行時尚無任何測試實例誕生。因此,這兩個方法必須定義爲靜態方法。
測試方法:
package com.example.project;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
/**
* @author zk
* @Description:
* @date 2019-10-25 14:16
*/
@DisplayName("第一個測試類") //展示的別名
public class MyFirstTest {
/**
* 每個單元測試前執行一次
*/
@AfterEach
void afterEach(){
System.out.println("afterEach");
}
@BeforeEach
void beforeEach(){
System.out.println("beforeEach");
}
/**
* BeforeAll 必須是static 的 一個類的所有單元測試開始前只執行一次
*/
@BeforeAll
static void beforeAll(){
System.out.println("beforeAll");
}
/**
* AfterAll 必須是static 的 一個類的所有單元測試後只執行一次
*/
@AfterAll
static void afterAll(){
System.out.println("afterall");
}
@DisplayName("第一個測試方法")
@Test
void test1(){
System.out.println("test1");
Assertions.assertEquals(2,1+1);
}
@DisplayName("第二個測試方法")
@Test
void test2(){
System.out.println("test2");
Assertions.assertNull(null);
}
}
第二個測試:
package com.example.project;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.EnumSource;
import org.junit.jupiter.params.provider.ValueSource;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
import static org.junit.jupiter.api.Assertions.*;
/**
* @author zk
* @Description:
* @date 2019-10-25 14:25
*/
@DisplayName("第二個測試類")
public class MySecondTest {
@Test
void test1(){
assertTrue(2 == 2, () -> "Assertion messages can be lazily evaluated -- "
+ "to avoid constructing complex messages unnecessarily.");
}
/**
* 多個測試
*/
@Test
void test2(){
Assertions.assertAll("all",
()->Assertions.assertEquals(2,1+1),
()->Assertions.assertNotEquals(2,1+3));
}
/**
* 超時的測試
*/
@Test
void timeoutNotExceeded() {
Assertions.assertTimeout(Duration.ofSeconds(3), () -> {
// Perform task that takes less than 3s
Thread.sleep(3500);
});
}
/**
* 測試方法提供參數
* @param argument
*/
@ParameterizedTest
@ValueSource(ints = { 1, 2, 3 })
void testWithValueSource(int argument) {
assertTrue(argument > 0 && argument < 4);
}
@ParameterizedTest
@EnumSource(TimeUnit.class)
void testWithEnumSource(TimeUnit timeUnit) {
assertNotNull(timeUnit);
}
/**
* 這裏測試報錯... https://blog.csdn.net/baidu_27222643/article/details/75020104
* @param fruit
* @param rank
*/
@ParameterizedTest
@CsvSource({
"apple, 1",
"banana, 2",
"'lemon, lime', 0xF1"
})
void testWithCsvSource(String fruit, int rank) {
assertNotNull(fruit);
assertNotEquals(0, rank);
}
}
好了, junit 的測試就說到這裏, 相比於junit4, 功能增加了好多.