Even though I'm using JUnit5 in my pom, the maven Surefire plugin still defaults to JUnit4. This can cause some problems, especially when using new features of JUnit5. To solve this problem, we can tell the Surefire plugin to use JUnit5 as the default test runner by configuring the pom file. In the build tag of the pom file, we need to add a plugins tag and configure maven-surefire-plugin in it. In the configuration, we need to specify testFrameworkProviderName as org.junit.platform.surefire.provider.JUnitPlatformProvider. This way, when we run the mvn test command, the Surefire plugin will use JUnit5 to run the tests. This configuration ensures that we can run tests normally while using JUnit5.
I have read this article. This is my parent 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>
In my subproject I don't have any junit
dependencies. When I run mvn install
I see the following:
[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]
Why is the automatically detected provider org.apache.maven.surefire.junit4.junit4provider
? It should use junit5!
When I run mvn dependency:tree
I see this:
[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
This is also weird because I'm not using versions 5.9.3
and 1.9.3
in pom.xml
. I'm using junit-bom:5.10.1
.
What's the reason and why maven doesn't work properly at once?
This is because:
[INFO] +- org.junit.platform:junit-platform-runner:jar:1.9.3:test [INFO] | +- junit:junit:jar:4.13.2:test
In the dependency tree. Removing the dependency on junit-platform-runner
in pom.xml
resolved the issue.
In my case this was because I also referenced spring-boot-dependency:3.1.5
in my dependencymanagement
which took precedence On junit-bom:5.10.1
. spring-boot-dependency:3.1.5
has a passing reference to junit-bom:5.9.3
.
The above is the detailed content of Even though I'm using JUnit5 in my pom, the maven Surefire plugin still defaults to JUnit4. For more information, please follow other related articles on the PHP Chinese website!