即使我在 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:
<dependencymanagement>
<dependencies>
<dependency>
<groupid>org.junit</groupid>
<artifactid>junit-bom</artifactid>
<version>5.10.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
...
<dependencies>
<dependency>
<groupid>org.junit.jupiter</groupid>
<artifactid>junit-jupiter-engine</artifactid>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-launcher -->
<dependency>
<groupid>org.junit.platform</groupid>
<artifactid>junit-platform-launcher</artifactid>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-runner -->
<dependency>
<groupid>org.junit.platform</groupid>
<artifactid>junit-platform-runner</artifactid>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-console-standalone -->
<dependency>
<groupid>org.junit.platform</groupid>
<artifactid>junit-platform-console-standalone</artifactid>
<version>1.10.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-commons -->
<dependency>
<groupid>org.junit.platform</groupid>
<artifactid>junit-platform-commons</artifactid>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-engine -->
<dependency>
<groupid>org.junit.platform</groupid>
<artifactid>junit-platform-engine</artifactid>
<scope>test</scope>
</dependency>
...
<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-surefire-plugin</artifactid>
<version>3.2.1</version>
<configuration>
<argline>--enable-preview --add-modules jdk.incubator.vector</argline>
</configuration>
</plugin>在我的子项目中,我没有任何 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.3 和 1.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.1。 spring-boot-dependency:3.1.5 具有对 junit-bom:5.9.3 的传递引用。
以上就是即使我在 pom 中使用 JUnit5,maven Surefire 插件仍然默认为 JUnit4的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号