'Maven not running JUnit 5 tests

I'm trying to get a simple junit test running with maven but it is not detecting any tests. Where am I going wrong? The project directory

Project -> src -> test-> java -> MyTest.java

Results :

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

pom.xml

<?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>com.buildproftest.ecs</groupId>
    <artifactId>buildprofiletest</artifactId>
    <version>1.0-SNAPSHOT</version>
    
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.3.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <debug>false</debug>
                    <optimize>true</optimize>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Junit test case

import org.junit.jupiter.api.Test;

public class MyTest {
    @Test
    public void printTest() {
        System.out.println("Running JUNIT test");
    }
}

The response is that there are no test cases to run.



Solution 1:[1]

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.

Solution 2:[2]

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:

…to run JUnit 5 tests.

This new artifact is a aggregate of other artifacts, a convenient wrapper to simplify your POM file.

    <dependencies>

        <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
        <!-- Provides everything you need to write JUnit 5 Jupiter tests. -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.7.0-M1</version>
            <scope>test</scope>
        </dependency>
        
    </dependencies>

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

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 a second dependency, junit-vintage-engine.

<dependencies>

    <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
    <!-- Provides everything you need to write JUnit 5 Jupiter tests. -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>5.7.0-M1</version>
        <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.junit.vintage/junit-vintage-engine -->
    <!-- Enables any legacy JUnit 3 and JUnit 4 tests you may have. Not needed for JUnit 5 tests. -->
    <dependency>
        <groupId>org.junit.vintage</groupId>
        <artifactId>junit-vintage-engine</artifactId>
        <version>5.7.0-M1</version>
        <scope>test</scope>
    </dependency>

</dependencies>

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. Currently at version 3.0.0-M5.

Solution 3:[3]

  • 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>

Solution 4:[4]

Add "include" in configuration section for maven surfire, your test should end with Test.java and it should work

<groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M5</version>
            <configuration>
                <includes>
                    <include>**/*Test.java</include>
                </includes>
            </configuration>

Solution 5:[5]

I recently discovered that I had to add the prefix "test" on my tests methods in order to get my tests executed by maven.

@Test
public void testStringsAreEqual() {
    var aString = "Walrus Code";

    Assertions.assertEquals(aString, "Walrus Code");
}

Otherwise the tests are ignored... This is part of my pom:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>11</source>
                <target>11</target>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>3.0.0</version>
            <configuration>
                <mainClass>com.myapp.Main</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>5.7.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>

Solution 6:[6]

In my case, on top of the accepted answer with 2 changes (maven-surefire plugin version and artifacts of org.junit.jupiter), I had to add one more change. (Java version used: 11)

Even though org.jacoco.agent was a part of my dependencies list, it needed to be added within < dependencyManagement > tag as well. This seemed to have resolved any underlying version/enforced the dependency automatically.

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.jacoco</groupId>
            <artifactId>org.jacoco.agent</artifactId>
            <version>${jacoco.version}</version>
            <classifier>runtime</classifier>
            <scope>test</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Basil Bourque
Solution 2
Solution 3 Tristan Elliott
Solution 4 Milicko
Solution 5 Codigo Morsa
Solution 6 VinjaNinja