DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernizing APIs: Share your thoughts on GraphQL, AI, microservices, automation, and more for our April report (+ enter a raffle for $250!).

DZone Research Report: A look at our developer audience, their tech stacks, and topics and tools they're exploring.

Getting Started With Large Language Models: A guide for both novices and seasoned practitioners to unlock the power of language models.

Managing API integrations: Assess your use case and needs — plus learn patterns for the design, build, and maintenance of your integrations.

Related

  • Integrate Cucumber in Playwright With Java
  • Readability in the Test: Exploring the JUnitParams
  • Creating Your Swiss Army Knife on Java Test Stack
  • I Don’t TDD: Pragmatic Testing With Java

Trending

  • Low-Code/No-Code Platforms: Seven Ways They Empower Developers
  • Applying CI/CD to Java Apps Using Spring Boot
  • Modern gRPC Microservices: REST Gateways, Part 2
  • Five Java Developer Must-Haves for Ultra-Fast Startup Solutions
  1. DZone
  2. Coding
  3. Java
  4. Why Your JUnit 5 Tests Are Not Running Under Maven

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?

John Thompson user avatar by
John Thompson
·
Nov. 21, 18 · Presentation
Like (19)
Save
Tweet
Share
191.6K Views

Join the DZone community and get the full member experience.

Join For Free

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!

Apache Maven JUnit Testing

Published at DZone with permission of John Thompson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Integrate Cucumber in Playwright With Java
  • Readability in the Test: Exploring the JUnitParams
  • Creating Your Swiss Army Knife on Java Test Stack
  • I Don’t TDD: Pragmatic Testing With Java

Comments

Partner Resources

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: