Maven not running JUnit 5 tests

  • I was having a similar problem where the junit @Test annotation would work but not the org.junit.jupiter.api.Test @Test annotation.

The solution:

  • THIS is mavens official guide on how to confugure the maven-surefire-plugin and make sure that is uses the proper junit-jupiter-engine

  • The guide is a little confusing so, this is how I got it to work

1) add the engine dependency to the dependencies block:

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.6.2</version>
        <scope>test</scope>
    </dependency>

2) add the engine dependency to maven-surefire:

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M6</version>
            <dependencies>
                <dependency>
                    <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-engine</artifactId>
                    <version>5.6.2</version>
                </dependency>
            </dependencies>
        </plugin>


According to the annotation (import org.junit.jupiter.api.Test), you are trying to run JUnit 5 tests with Maven. According to the documentation, you have to add this dependency:

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.3.1</version>
    <scope>test</scope>
</dependency>

Your version of Maven comes with a version of maven-surefire-plugin which does not support JUnit 5. You could update your Maven to the latest version. You could also set the version of the maven-surefire-plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <!-- JUnit 5 requires Surefire version 2.22.0 or higher -->
    <version>2.22.0</version>
</plugin>

See the junit5-samples for this information.

See the Maven Surefire Plugin artifact in a Maven repository. At version 3.0.0-M3 as of 2019-01.


tl;dr

Add two dependencies to your project:

  • JUnit Jupiter (Aggregator)
  • Maven Surefire Plugin
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <version>5.9.1</version>
    <scope>test</scope>
</dependency>

… and:

<!-- https://maven.apache.org/surefire/maven-surefire-plugin/dependency-info.html -->
<dependency>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>3.0.0-M7</version>
  <type>maven-plugin</type>
</dependency>

junit-jupiter — the simpler archetype for JUnit 5

The Answer by LaurentG seems to be correct, but a bit outdated.

As of JUnit 5.4, you can replace those multiple Maven artifacts:

  • junit
  • junit-jupiter-api
  • junit-jupiter-engine

…with a single artifact:

  • junit-jupiter

…to run JUnit 5 tests.

This artifact is an aggregate of other artifacts, a convenient wrapper to simplify your POM file. See this repository.

<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <version>5.9.1</version>
    <scope>test</scope>
</dependency>

This gives you all you need to write and run JUnit 5 Jupiter tests.

maven-surefire-plugin

You would also need the Surefire plugin as shown in that other Answer. Be sure to get the latest version, as Surefire has had some important fixes/enhancements recently.

See this list of dependency declarations for Maven, Buildr, Ivy, Grape, Grails, SBT, and Leiningen. To quote the Maven config:

<!-- https://maven.apache.org/surefire/maven-surefire-plugin/dependency-info.html -->
<dependency>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>3.0.0-M7</version>
  <type>maven-plugin</type>
</dependency>

junit-vintage-engine for JUnit 3 & 4 tests

If you have old JUnit 3 or JUnit 4 legacy tests that you want to continue to run, add another dependency, junit-vintage-engine.

<!-- https://mvnrepository.com/artifact/org.junit.vintage/junit-vintage-engine -->
<dependency>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    <version>5.9.1</version>
    <scope>test</scope>
</dependency>