首页  >  文章  >  即使我在 pom 中使用 JUnit5,maven Surefire 插件仍然默认为 JUnit4

即使我在 pom 中使用 JUnit5,maven Surefire 插件仍然默认为 JUnit4

WBOY
WBOY转载
2024-02-13 14:00:10405浏览

即使我在 pom 中使用 JUnit5,maven Surefire 插件仍然默认为 JUnit4。这可能会导致一些问题,特别是在使用 JUnit5 的新特性时。为了解决这个问题,我们可以通过配置 pom 文件来告诉 Surefire 插件使用 JUnit5 作为默认测试运行器。在 pom 文件的 build 标签中,我们需要添加一个 plugins 标签,并在其中配置 maven-surefire-plugin。在配置中,我们需要指定 testFrameworkProviderName 为 org.junit.platform.surefire.provider.JUnitPlatformProvider。这样,当我们运行 mvn test 命令时,Surefire 插件将使用 JUnit5 来运行测试。这个配置可以确保我们在使用 JUnit5 的同时,还能够正常运行测试。

问题内容

我读过这篇文章。这是我的父 pom:




            org.junit
            junit-bom
            5.10.1
            pom
            import
        
...
    

        org.junit.jupiter
        junit-jupiter-engine        
        test
    

        

    org.junit.platform
    junit-platform-launcher    
    test




    org.junit.platform
    junit-platform-runner    
    test




    org.junit.platform
    junit-platform-console-standalone
    1.10.1
    test




    org.junit.platform
    junit-platform-commons    
    test




    org.junit.platform
    junit-platform-engine    
    test

...

                org.apache.maven.plugins
                maven-surefire-plugin
                3.2.1
                
            --enable-preview --add-modules jdk.incubator.vector
        
            

在我的子项目中,我没有任何 junit 依赖项。当我运行 mvn install 时,我看到以下内容:

[info] --- surefire:3.2.1:test (default-test) @ xxx ---
[info] toolchain in maven-surefire-plugin: jdk[/library/java/jdk-21.0.1.jdk/contents/home]
[info] using auto detected provider org.apache.maven.surefire.junit4.junit4provider
[info]
[info] -------------------------------------------------------
[info]  t e s t s
[info] -------------------------------------------------------
warning: using incubator modules: jdk.incubator.vector
[info]
[info] results:
[info]
[info] tests run: 0, failures: 0, errors: 0, skipped: 0
[info]

为什么自动检测到的提供程序是 org.apache.maven.surefire.junit4.junit4provider?它应该使用 junit5!

当我运行 mvn dependency:tree 时,我看到这个:

[INFO] --- dependency:3.6.0:tree (default-cli) @ xxx ---
[INFO] xxx:xxx:jar:1.0.0-SNAPSHOT
[INFO] +- org.eclipse.collections:eclipse-collections:jar:11.1.0:compile
[INFO] |  \- org.eclipse.collections:eclipse-collections-api:jar:11.1.0:compile
[INFO] +- org.slf4j:slf4j-api:jar:2.0.9:compile
[INFO] +- com.google.guava:guava:jar:32.1.2-jre:compile
[INFO] |  +- com.google.guava:failureaccess:jar:1.0.1:compile
[INFO] |  +- com.google.guava:listenablefuture:jar:9999.0-empty-to-avoid-conflict-with-guava:compile
[INFO] |  +- com.google.code.findbugs:jsr305:jar:3.0.2:compile
[INFO] |  +- com.google.errorprone:error_prone_annotations:jar:2.18.0:compile
[INFO] |  \- com.google.j2objc:j2objc-annotations:jar:2.8:compile
[INFO] +- org.assertj:assertj-core:jar:3.24.2:test
[INFO] |  \- net.bytebuddy:byte-buddy:jar:1.14.9:test
[INFO] +- org.junit.jupiter:junit-jupiter-engine:jar:5.9.3:test
[INFO] |  +- org.junit.jupiter:junit-jupiter-api:jar:5.9.3:test
[INFO] |  \- org.apiguardian:apiguardian-api:jar:1.1.2:test
[INFO] +- org.junit.platform:junit-platform-launcher:jar:1.9.3:test
[INFO] +- org.junit.platform:junit-platform-runner:jar:1.9.3:test
[INFO] |  +- junit:junit:jar:4.13.2:test
[INFO] |  |  \- org.hamcrest:hamcrest-core:jar:2.2:test
[INFO] |  |     \- org.hamcrest:hamcrest:jar:2.2:test
[INFO] |  +- org.junit.platform:junit-platform-suite-api:jar:1.9.3:test
[INFO] |  \- org.junit.platform:junit-platform-suite-commons:jar:1.9.3:test
[INFO] +- org.junit.platform:junit-platform-console-standalone:jar:1.10.1:test
[INFO] +- org.junit.platform:junit-platform-commons:jar:1.9.3:test
[INFO] +- org.junit.platform:junit-platform-engine:jar:1.9.3:test
[INFO] |  \- org.opentest4j:opentest4j:jar:1.2.0:test
[INFO] \- org.checkerframework:checker-qual:jar:3.42.0:compile

这也很奇怪,因为我在 pom.xml 中没有使用版本 5.9.31.9.3 。我正在使用 junit-bom:5.10.1

什么原因以及为什么 maven 一次不能正常工作?

解决方法

这是因为:

[INFO] +- org.junit.platform:junit-platform-runner:jar:1.9.3:test
[INFO] |  +- junit:junit:jar:4.13.2:test

在依赖树中。删除 pom.xml 中对 junit-platform-runner 的依赖解决了该问题。

就我而言,这是因为我还引用了 spring-boot-dependency:3.1.5 在我的 dependencymanagement 中,它优先于 junit-bom:5.10.1spring-boot-dependency:3.1.5 具有对 junit-bom:5.9.3 的传递引用。

以上是即使我在 pom 中使用 JUnit5,maven Surefire 插件仍然默认为 JUnit4的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:stackoverflow.com。如有侵权,请联系admin@php.cn删除