Unable to execute Junit5 tests using Maven

1.6k views Asked by At

Maven execution

mvn clean test

I am trying to use junit5 for one of my maven projects but not able to execute the unit tests during the test phase using -

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.0.0-M3</version>
</dependency>

The output that I get is -

[INFO] --- maven-surefire-plugin:2.19.1:test (default-test) @ utils ---

-------------------------------------------------------
 T E S T S
-------------------------------------------------------

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

Tried implementing the solution mentioned @ Surefire is not picking up Junit 5 tests to update the dependencies to -

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.0.0-M3</version>
</dependency>
<dependency>
    <groupId>org.junit</groupId>
    <artifactId>junit5-api</artifactId>
    <version>5.0.0-ALPHA</version><!--https://mvnrepository.com/artifact/org.junit/junit5-api -->
    <scope>test</scope>
</dependency>

and updating the plugin to -

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19.1</version>
    <dependencies>
        <dependency>
            <groupId>org.junit</groupId>
            <artifactId>surefire-junit5</artifactId>
            <version>5.0.0-ALPHA</version><!--couldn't find this on the central though-->
        </dependency>
    </dependencies>
</plugin>

but the output remains same.

Question - Is this custom provider no more supported or is there any solution to executing the tests using maven, junit5 and/or junit5-api currently?

Note - The test execution was working fine with JUnit-4.

1

There are 1 answers

0
Marc Philipp On BEST ANSWER

You should configure the maven-surefire-plugin like this:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19.1</version>
    <dependencies>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-surefire-provider</artifactId>
            <version>1.0.0-M3</version>
        </dependency>
    </dependencies>
</plugin>

You only need to include the junit-jupiter-api artifact and only in test scope in your dependencies section:

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.0.0-M3</version>
    <scope>test</scope>
</dependency>

Please see http://junit.org/junit5/docs/current/user-guide/#running-tests-build-maven for more information.

Edit - From the same docs.

In order to have Maven Surefire run any tests at all, a TestEngine implementation must be added to the runtime classpath.

<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19</version>
    <dependencies>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-surefire-provider</artifactId>
            <version>1.0.0-M3</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.0.0-M3</version>
        </dependency>
    </dependencies>
</plugin>