Why Your JUnit 5 Tests Are Not Running Under Maven

Why Your JUnit 5 Tests Are Not Running Under Maven

9 Comments

Last Updated on June 3, 2019 by Simanta

So your JUnit 5 tests are not running under Maven?

You have JUnit 5 Tests which run fine from your IDE, but fail to run under Maven?

Your test output looks like this:

 [INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.741 s
[INFO] Finished at: 2018-11-17T05:34:18-05:00
[INFO] Final Memory: 12M/54M
[INFO] ------------------------------------------------------------------------

The Cause

The root cause is likely your Maven version. Maven 3.6.0 was released on October 24th, 2018. This release includes version 2.22.0 of the Maven Surefire Plugin (unit test runner), and 2.22.0 of the Maven Failsafe (integration test runner) plugin. The 2.22.0 releases include support for JUnit.

Prior to these releases, to run Junit 5 tests under Maven, you needed to include a JUnit provider dependency for the Maven Surefire plugin.

You will see example configurations for Maven 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.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.1.0</version>
        </dependency>
    </dependencies>
</plugin>

This is correct for pre 2.22.0 releases of Maven Surefire/Failsafe.

Under this configuration you will see the expected output of your JUnit 5 tests running:

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.015 sec - in guru.springframework.GreetingTest

Results :

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

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.624 s
[INFO] Finished at: 2018-11-17T05:49:27-05:00
[INFO] Final Memory: 12M/44M
[INFO] ------------------------------------------------------------------------

However, if you update Surefire to 2.22.0 like this:

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.0</version>
                <dependencies>
                    <dependency>
                        <groupId>org.junit.platform</groupId>
                        <artifactId>junit-platform-surefire-provider</artifactId>
                        <version>1.1.0</version>
                    </dependency>
                    <dependency>
                        <groupId>org.junit.jupiter</groupId>
                        <artifactId>junit-jupiter-engine</artifactId>
                        <version>5.1.0</version>
                    </dependency>
                </dependencies>
            </plugin>

Your JUnit 5 tests wont run:

[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.604 s
[INFO] Finished at: 2018-11-17T05:51:10-05:00
[INFO] Final Memory: 13M/54M
[INFO] ------------------------------------------------------------------------

Apparently there is some conflict between the JUnit Surefire provider and the JUnit support in the Surefire 2.22.0 plugin release.

I ran into this little ‘feature‘ while developing my Testing Spring Boot – Beginner to Guru course.

junit 5 tests with maven

The Simple Solution

The solution is simple, use Maven Release 3.6.0.

Problems with the Simple Solution – Maven is Boring

However, in a distributed environment, you cannot guarantee everyone is using Maven 3.6.0.

Your co-worker might be on an old release of Maven. Your CI server may be running an older release of Maven.

I’m a big fan of Circle CI. At the time of writing, their base Docker build image is running an older version of Maven. (thus your JUnit 5 tests will fail to run on Circle CI)

I personally just updated my personal MBP to Maven 3.6.0.

People just don’t update their Maven installation very often.

Let’s face it, Maven is so stable its boring.

maven is boring

One solution would be to use Maven wrapper in your project – solid approach.

If everyone would also use the Maven wrapper script.

They won’t.

The Foolproof Solution

I recommend to foolproof this problem, and just update your POM to require the 2.22.0 releases of the Maven Surefire and Failsafe plugins.

Here is an example configuration:

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.0</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.22.0</version>
            </plugin>
        </plugins>
    </build>

Here is a complete Maven POM for JUnit 5 and Java 11:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>guru.springframework</groupId>
    <artifactId>intro-junit5</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>intro-junit5</name>
    <description>Introduction to JUnit 5</description>

    <organization>
        <name>Spring Framework Guru</name>
        <url>http://springframework.guru/</url>
    </organization>

    <developers>
        <developer>
            <id>jt</id>
            <name>John Thompson</name>
            <email>[email protected]</email>
        </developer>
    </developers>

    <inceptionYear>2018</inceptionYear>

    <licenses>
        <license>
            <name>The Apache License, Version 2.0</name>
            <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
        </license>
    </licenses>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>11</java.version>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
        <junit-platform.version>5.3.1</junit-platform.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit-platform.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit-platform.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.0</version>
                <configuration>
                    <argLine>
                        --illegal-access=permit
                    </argLine>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.22.0</version>
                <configuration>
                    <argLine>
                        --illegal-access=permit
                    </argLine>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

This POM is an example JUnit 5 project from my Testing Spring Boot – Beginner to Guru course. You can find the complete source code for this project on GitHub (make sure you are on the branch ‘hello-world-test’.

This is a quirky little problem. Hopefully, if you found your JUnit 5 tests are not running under Maven this post helped you out!

About jt

    You May Also Like

    9 comments on “Why Your JUnit 5 Tests Are Not Running Under Maven

    1. November 26, 2018 at 9:50 am

      Hello,

      You wrote “The solution is simple, use Maven Release 3.6.0.” but did you mean Apache Maven Compiler Plugin 3.8.0, please ?

      Reply
      • November 26, 2018 at 10:01 am

        No I did not mean that.

        Reply
    2. December 17, 2018 at 6:52 am

      Thanks, i was wondering about it and found the solution. awesome

      Reply
    3. January 14, 2019 at 9:48 am

      Thanks, workaround with 2.22.0 really helps!

      Reply
    4. August 29, 2019 at 8:44 am

      Stepping up to surefire 2.22.0 fixed my issue with an InterruptedException being thrown when a HystrixCommmand is being tested.
      Thanks a lot!

      Reply
      • January 28, 2020 at 8:17 am

        Does anyone know how to configure it with Spring Boot?
        I have “spring-boot-maven-plugin” ver 2.2.4.RELEASE, “maven-surefire-plugin” ver 2.2.4.RELEASE and it won’t work unless I add “junit-platform-surefire-provider” as the dependency into “maven-surefire-plugin”.
        I get a warning about deprecatation, but it just won’t work without it.
        Thanks!

        Reply
    5. January 28, 2020 at 5:33 pm

      Have you been able to implement running test classes concurrently with JUnit5 and surefire plugin?

      Reply
    6. July 25, 2024 at 2:22 pm

      Nope, that’s not my problem. I’ve also tried everything listed in https://stackoverflow.com/questions/53433663/maven-not-running-junit-5-tests

      Reply

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.