{{announcement.body}}
{{announcement.title}}

Why Your JUnit 5 Tests Are Not Running Under Maven

DZone 's Guide to

Why Your JUnit 5 Tests Are Not Running Under Maven

You've made the switch to JUnit 5 and you couldn't be happier. Or could you?

· Java Zone ·
Free Resource

So your JUnit 5 tests are not running under Maven? Do you have JUnit 5 tests that 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 won't 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.

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 that it's boring.

One solution would be to use Maven wrapper in your project; this is a solid approach, if everyone would also use the Maven wrapper script.

They won't.

The Foolproof Solution

I recommend to use 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>https://springframework.guru/</url>
    </organization>

    <developers>
        <developer>
            <id>jt</id>
            <name>John Thompson</name>
            <email>john@springframework.guru</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!

Topics:
java ,junit 5 ,maven ,problem ,solution

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}