單元測試系列之六:JUnit5 技術前瞻

更多原創測試技術文章同步更新到微信公衆號 :三國測,敬請掃碼關注我的的微信號,感謝!html

 

原文連接:http://www.cnblogs.com/zishi/p/6868495.html
 
JUnit 5是下一代JUnit。 目標是爲JVM上的開發人員端測試建立一個最新的基礎。 這包括專一於Java 8及更高版本,以及啓用許多不一樣風格的測試。
 
 
JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage
與之前的JUnit版本不一樣,JUnit 5由三個不一樣的子項目組成
 
支持的Java版本
JUnit 5在運行時須要Java 8。 可是,仍然能夠測試使用之前版本的JDK編譯的代碼。
 
JUnit平臺做爲在JVM上啓動測試框架的基礎。 它還定義了用於開發在平臺上運行的測試框架的TestEngine API。 此外,該平臺提供了一個控制檯啓動器,從命令行啓動平臺,爲Gradle和Maven構建插件以及基於JUnit 4的Runner,用於在平臺上運行任何TestEngine。
 
JUnit Jupiter是用於在JUnit 5中編寫測試和擴展的新編程模型和擴展模型的組合.Jupiter子項目提供了一個用於在平臺上運行基於Jupiter的測試的TestEngine。
 
JUnit Vintage提供了一個用於在平臺上運行JUnit 3和JUnit 4的測試的TestEngine。
 
JUnit 5團隊於2017年4月1日發佈了里程碑4,目前正在開發額外的里程碑,最終發佈GA版本(詳見路線圖Roadmap)。
 
JUnit Jupiter帶有許多JUnit 4的斷言方法,並添加了一些能夠與Java 8 lambdas一塊兒使用的方法。
 
全部JUnit Jupiter斷言是org.junit.jupiter.Assertions類中的靜態方法.
新特性是分組斷言,這在之前Junit4是不被容許的。
如下是斷言的Demo:
 
import static java.time.Duration.ofMillis;
import static java.time.Duration.ofMinutes;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTimeout;
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

class AssertionsDemo {

    @Test
    void standardAssertions() {
        assertEquals(2, 2);
        assertEquals(4, 4, "The optional assertion message is now the last parameter.");
        assertTrue(2 == 2, () -> "Assertion messages can be lazily evaluated -- "
                + "to avoid constructing complex messages unnecessarily.");
    }


    @Test
    void groupedAssertions() {
 //分組斷言:在一個分組斷言中,全部斷言都會被執行,而且任何失敗的斷言也會一塊兒被報告
        assertAll("address",
            () -> assertEquals("John", address.getFirstName()),
            () -> assertEquals("User", address.getLastName())
        );
    }

    @Test
    void exceptionTesting() {
        Throwable exception = assertThrows(IllegalArgumentException.class, () -> {
            throw new IllegalArgumentException("a message");
        });
        assertEquals("a message", exception.getMessage());
    }
@Test
    void timeoutNotExceeded() {
        // 下面的斷言成功
        assertTimeout(ofMinutes(2), () -> {
            // 執行任務只需不到2分鐘
        });
    }

    @Test
    void timeoutNotExceededWithResult() {
        // 如下斷言成功,並返回所提供的對象
        String actualResult = assertTimeout(ofMinutes(2), () -> {
            return "a result";
        });
        assertEquals("a result", actualResult);
    }

    @Test
    void timeoutNotExceededWithMethod() {
        // 如下斷言調用方法參照並返回一個對象
        String actualGreeting = assertTimeout(ofMinutes(2), AssertionsDemo::greeting);
        assertEquals("hello world!", actualGreeting);
    }
@Test
    void timeoutExceeded() {
        // 下面的斷言失敗,相似的錯誤信息:
        // execution exceeded timeout of 10 ms by 91 ms
        assertTimeout(ofMillis(10), () -> {
            // 模擬的任務,須要超過10毫秒
            Thread.sleep(100);
        });
    }

    @Test
    void timeoutExceededWithPreemptiveTermination() {
        // 下面的斷言失敗,相似的錯誤信息:
        // execution timed out after 10 ms
        assertTimeoutPreemptively(ofMillis(10), () -> {
            // 模擬的任務,須要超過10毫秒
            Thread.sleep(100);
        });
    }

    private static String greeting() {
        return "hello world!";
    }

}

 

即便由JUnit Jupiter提供的斷言設施對於許多測試場景也是足夠的,但有時候須要或須要更多的功率和附加功能,如匹配器。
在這種狀況下,JUnit團隊建議使用第三方斷言庫,如AssertJ,Hamcrest,Truth等。所以,開發人員能夠自由使用他們選擇的斷言庫。
 
例如,匹配器和流暢的API的組合可用於使斷言更具描述性和可讀性。 可是,JUnit Jupiter的org.junit.jupiter.Assertions類不提供像JUnit 4的org.junit.Assert類中的assertThat()方法,它接受Hamcrest Matcher。 相反,鼓勵開發人員使用第三方斷言庫提供的匹配器的內置支持。
 
如下示例演示瞭如何在JUnit Jupiter測試中使用Hamcrest的assertThat()支持。 只要將Hamcrest庫添加到類路徑中,您能夠靜態導入assertThat(),is()和equalsTo()等方法,而後在下面的assertWithHamcrestMatcher()方法中使用它們。
Demo:
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

class HamcrestAssertionDemo {

    @Test
    void assertWithHamcrestMatcher() {
        assertThat(2 + 1, is(equalTo(3)));
    }

}

 

附錄:參考java

 官網首頁: http://junit.org/junit5/
 
  做者原創技術文章,轉載請註明出處

其餘推薦相關閱讀:git

 

單元測試系列之一:如何使用JUnit、JaCoCo和EclEmma提升單元測試覆蓋率

 

測試系列之二:Mock工具Jmockit實戰github

 

單元測試系列之三:JUnit單元測試規範數據庫

 

單元測試系列之四:Sonar平臺中項目主要指標以及代碼壞味道詳解編程

 

單元測試系列之五:Mock工具之Mockito實戰api

 

單元測試系列之六:JUnit5 技術前瞻微信

 

單元測試系列之七:Sonar 數據庫表關係整理一(rule相關)框架

 

單元測試系列之八:Sonar 數據庫表關係整理一(續)工具

 

單元測試系列之九:Sonar 經常使用代碼規則整理(一)

 

單元測試系列之十:Sonar 經常使用代碼規則整理(二)

 

單元測試系列之十一:Jmockit之mock特性詳解

相關文章
相關標籤/搜索