javamavenjunit

maven surefire plugin keeps on defaulting to JUnit4 even though I am using JUnit5 in my pom


I have read this. Here 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 child project I don't have any junit dependencies. When I run mvn install I see this:

[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 auto detected provider org.apache.maven.surefire.junit4.JUnit4Provider? Its supposed to 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 as nowhere I am using versions 5.9.3 and 1.9.3 in my pom.xml. I am using junit-bom:5.10.1.

What gives and why can't maven work correctly for once?


Solution

  • Why is the auto detected provider org.apache.maven.surefire.junit4.JUnit4Provider? Its supposed to use JUnit5!

    This is because of:

    [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 dependency on junit-platform-runner in pom.xml fixed the problem.

    This is also weird as nowhere I am using versions 5.9.3 and 1.9.3 in my pom.xml. I am using junit-bom:5.10.1.

    In my case this is because I also had a reference to spring-boot-dependencies:3.1.5 in my dependencyManagement and it was taking precedence over junit-bom:5.10.1. spring-boot-dependencies:3.1.5 has transitive reference to junit-bom:5.9.3.