mvn test運行Junit5測試用例時檢測不到測試類的問題


問題再現:

使用 Intellij Idea 開發工具,每次執行單個 Junit5 測試時一切正常。

但是在項目目錄中,執行 mvn test,或者 Intellij 右邊的 Maven 打開后執行 Lifecycle 的 test 時,出現問題:

查閱文檔

Junit5-User Guide-Get Started 給出了一些 Junit5 的例子。

其中,junit5-jupiter-starter-maven pom.xml 中給了我一些提示!

修改 pom.xml

  1. 依賴 junit-jupiter-engine
  2. 新增插件 maven-surefire-plugin
<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>org.coderead</groupId>
    <artifactId>test-junit5</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>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.7.1</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>

參考博客

解決mvn test運行Junit5測試用例時檢測不到測試類的問題


免責聲明!

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



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