Why Your JUnit 5 Tests Are Not Running Under Maven

Why Your JUnit 5 Tests Are Not Running Under Maven

3 Comments

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:

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:

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:

However, if you update Surefire to 2.22.0 like this:

Your JUnit 5 tests wont run:

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:

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

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!

0

About jt

    You May Also Like

    3 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

    Leave a Reply