咱的Maven項目能用Junit5嗎?


1. 簡介

不同於 JUnit3 或者 JUnit4,JUnit 5 由多個模塊組成,並三個核心的子項目:

JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage

查看一下官網 go JUnit5,上面有最新版本顯示:

這版本號可以幫助我們填寫下面的依賴 junit-platform-launcher AND junit-jupiter-engine AND junit-vintage-engine

<dependencies>

    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-launcher</artifactId>
        <version>1.7.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.7.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.vintage</groupId>
        <artifactId>junit-vintage-engine</artifactId>
        <version>5.7.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>

為了 mvn test 順利執行 Junit5 測試用例,那么你需要 Maven 插件 maven-surefire-plugin:

<plugins>
    <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.2</version>
    </plugin>
</plugins>

2. 思考

這三個子項目,我們每次都需要引入嗎?

  • 答案:不是的,實際還是需要因項目需求而異的。

3. 應用場景

首先項目必須 JDK8+

JUnit 5 requires Java 8 (or higher) at runtime.

  • 也就是說需要 JDK8+ 的項目才能用 JUnit5,否則老老實實選用 JUnit4 吧。

3.1 新項目用JUnit5

如果,我們是一個新開的項目,需要使用 Junit5,怎么選?

  • 答案:只需要引入 junit-jupiter-engine 即可。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>junit5-jupiter-starter-maven</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>${maven.compiler.source}</maven.compiler.target>

        <junit5.version>5.7.1</junit5.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit5.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
            </plugin>
        </plugins>
    </build>

</project>

3.2 遷移至JUnit5

如果,我們的項目是一個舊項目,用的是 JDK8 + JUnit4,現在新的測試用例想要用 JUnit5 來寫,怎么辦?

  • 答案:引入 junit-jupiter-engine AND junit-vintage-engine。原先的依賴 junit 可以考慮移除,因為 junit-vintage-engine 會為我們自動引入可傳遞依賴項 junit
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>junit5-migration-maven</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>${maven.compiler.source}</maven.compiler.target>

        <junit5.version>5.7.1</junit5.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit5.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
            <version>${junit5.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
            </plugin>
        </plugins>
    </build>

</project>

4. 解讀

要開始使用 JUnit5 Platform,您至少需要向項目中添加一個 TestEngine 實現。

junit-jupiter-engine 項目包含實現類 JupiterTestEnginejunit-vintage-engine 項目包含實現類 VintageTestEngine

如果您想用 Jupiter 編寫測試,請將測試件 junit-jupiter-engine 添加到 POM 中的依賴項中。這將引入所有必需的依賴項。在這些依賴關系中,就有 junit-jupiter-api,它包含測試源代碼編譯所需的類和接口。

我們通常使用的 @Test,@Disabled 等注解,Assertions 等斷言 API 都出自 junit-jupiter-api

我們在項目中引入依賴 junit-jupiter-engine 時,Maven 就會自動替我們引入 junit-jupiter-api

如果您想通過JUnit平台編寫和執行JUnit3或4測試,請將 junit-vintage-engine 添加到依賴項中,該引擎可傳遞地引入(並需要)junit:junit:4.12

引入依賴 junit-vintage-engine 時,Maven 就會自動替我們引入 junit

5. 為啥沒用到 junit-platform-launcher?

IntelliJ IDEA在IDEA 2017.3 之前發布了 JUnit 5 的特定捆綁包版本。因此,如果您想使用較新版本的Junit Jupiter,IDE中的測試執行可能會由於版本沖突而失敗。在這種情況下,請按照下面的說明使用比 IntelliJ IDEA 捆綁的JUnit 5更新的版本。

junit-platform-launcher 更多的是服務於 IDE 的,如果出現了版本沖突引起的測試執行失敗,就需要添加,反之我覺得不怎么需要呢?

因此,加或者不加這個 junit-platform-launcher 依賴都有一定的合理性。個人沒有遇過這類問題,所以暫時不加。

6. 參考文檔


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM