Java最受歡迎的測試庫JUnit發佈了一個新版本5.4。自上一次5.3.2發佈以來,這個版本帶來了一些值得咱們去嘗試的地方,本文中,我將介紹最重要的內容,並在適用的地方提供代碼示例。
你可能知道,JUnit 5.x(Jupiter)是對JUnit 4.x(Vintage)的巨大改進。JUnit 5是一個更優秀的版本,包括Lambda支持,JUnit 5擴展,測試方法參數注入以及許多其餘功能。JUnit 5.4是JUnit 5的演變,進一步改善了測試體驗。html
官方使用說明
JUnit 5 = JUnit平臺 + JUnit Jupiter + JUnit Vintageapi
如下是原文就不翻譯了。intellij-idea
The JUnit Platform serves as a foundation for launching testing frameworks on the JVM. It also defines the [TestEngine](https://junit.org/junit5/docs/current/api/org/junit/platform/engine/TestEngine.html)
API for developing a testing framework that runs on the platform. Furthermore, the platform provides a Console Launcher to launch the platform from the command line and a JUnit 4 based Runner for running any TestEngine
on the platform in a JUnit 4 based environment. First-class support for the JUnit Platform also exists in popular IDEs (see IntelliJ IDEA, Eclipse, NetBeans, and Visual Studio Code) and build tools (see Gradle, Maven, and Ant).框架
JUnit Jupiter is the combination of the new programming model and extension model for writing tests and extensions in JUnit 5. The Jupiter sub-project provides a TestEngine
for running Jupiter based tests on the platform.eclipse
JUnit Vintage provides a TestEngine
for running JUnit 3 and JUnit 4 based tests on the platform.maven
Junit 5 對應的JDK版本必定要是1.8以上ide
要開始使用JUnit 5.4,經過Maven引入。使用JUnit 5.4已經簡化了。而在先前版本的JUnit 5中,須要分別導入幾個Jar包(例如,jupiter-api,jupiter-params,jupiter-engine),新版本容許你經過包括僅僅單一的使用JUnit 5.4 junit-jupiter依賴性彙集工件。單元測試
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter --> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>5.4.1</version> <scope>test</scope> </dependency>
項目代碼結構測試
在測試時,有時你須要訪問臨時文件。JUnit 5如今提供了一個建立和清理臨時文件的擴展,而不是本身處理這樣一個文件的建立。您可使用它來放置@TempDirectory註釋Path或File測試輸入參數或字段。一個例子:gradle
@Test void writeItemsToFile(@TempDir Path tempDir) throws IOException { Path file = tempDir.resolve("test.txt"); new ListWriter(file).write("a", "b", "c"); assertEquals(singletonList("a,b,c"), Files.readAllLines(file)); }
參數化測試是爲了測試的時候提供好的入參,好比下面的的Limit就會直接打印Limit的全部枚舉列表
public enum Limit { IN(1,"包含"), NOT_IN(0,"不包含"); public final int value; public final String alias; Limit(int value, String k) { this.value = value; this.alias = k; } }
@ParameterizedTest @EnumSource(Limit.class) void checkLimit(Limit limit) { assertTrue(limitCheck.isValid(limit)); }
可是,在這樣的設置中,假如咱們須要去測試極端場景下,代碼的健壯性,模擬提供null元素有點困難,或者在其餘狀況下,提供空元素。@NullSource(並@EmptySource加@NullAndEmptySource)就能夠幫助到咱們。
@NullSource至關於Mock了一個Null值作爲入參,以此咱們能夠去測試代碼
@ParameterizedTest @NullSource // now also null is tested @EnumSource(Limit.class) void checkLimit(Limit limit) { assertTrue(limitCheck.isValid(limit)); }
一般,在使用測試類時,可使用@DisplayName註釋覆蓋測試方法名稱或類。例如,見下文
@Test @DisplayName("計算加法的一個單元測試") public void add() { }
雖然上面提供了更易讀的格式,但它很是靜態。您如今能夠作的是根據例如嵌套類或方法名稱生成顯示名稱。能夠在文檔中找到一個好的DisplayNameGeneration示例。
雖然一般將測試方法相互依賴並非一個好主意,但對於某些狀況,在測試執行中有必定的順序是很方便的。例如,一個測試可能在REST端點中建立資源,而其餘測試則驗證此資源的某些屬性。
在之前版本的JUnit中,這很難作到,可是從5.4開始,你可使用一個新TestMethodOrder命名的OrderAnnotation。你能夠以此來結合使用,Order以強制按特定順序執行測試方法。
@TestMethodOrder(OrderAnnotation.class) class SequenceTest { @Test @Order(1) void createResource() { // Create a resource first... } @Test @Order(2) void verify() { // ...then verify some attributes. } }
新的JUnit 5版本中還有許多其餘功能,能夠在官網找到。除了框架以外,JUnit 5的一大優勢是文檔很好。有不少可用的文檔,其中的概念用很是簡單的代碼示例進行了解釋,這一點我以爲和Spring的官網說明同樣具備高質量,簡單清晰。
若是您還沒有升級到JUnit 5,那麼最新版本提供了許多強大的功能,這些功能將使遷移變得有價值。