87

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.

abc
  • 23
  • 4
mogoli
  • 2,153
  • 6
  • 26
  • 41
  • It's good your test method is public, but the class can be package-private for JUnit 5+. See also https://www.baeldung.com/maven-cant-find-junit-tests – banan3'14 Jan 23 '23 at 10:48

9 Answers9

127

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
LaurentG
  • 11,128
  • 9
  • 51
  • 66
  • 5
    I added the dependency and ran mvn clean test but no test run still. Thanks. – mogoli Nov 22 '18 at 15:07
  • Why have you also the JUnit 4.12 as dependency? It might be the problem. Try to remove it. – LaurentG Nov 22 '18 at 15:11
  • 1
    I've removed that now and still no joy. I ran the test through my IDE independent of maven and it does run. Thanks – mogoli Nov 22 '18 at 15:13
  • I could reproduce the problem and fix it with the version of `maven-surefire-plugin`. – LaurentG Nov 22 '18 at 15:27
  • Manged to solve it, I wasnt specifying the version of the sure fire plugin. I specified 3.0.0-M1 and it worked. Thank you – mogoli Nov 22 '18 at 15:31
  • 5
    Tip: As of JUnit 5 version 5.4.0, you can use the new convenient single Maven artifact named *JUnit Jupiter (Aggregator)* that in turn gets you several related artifacts needed to write and run JUnit 5 tests: [`junit-jupiter`](https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter) – Basil Bourque Jan 29 '19 at 02:23
  • Just stumbled upon this answer. Is 3.0.0-M3 considered stable? I am not sure about that suffix in the version number and didn't find any information about the version scheme of the maven plugins. – Sebastian S Feb 20 '19 at 14:20
  • @SebastianS The official page claims, this is a stable version: https://maven.apache.org/surefire/download.cgi – LaurentG Feb 20 '19 at 17:07
  • It worked for me using the junit-jupiter dependency and the version 3.0.0 of the Surefire plugin; however, only the test classes located in the root package are run (by root package I mean the one having the same name as the groupId in the pom) – Pedro García Medina Jul 02 '20 at 01:46
  • Adding dependency `junit-jupiter-engine` did the trick for me. I had had already `junit-jupiter-api` and `maven-surefire-plugin`. – havryliuk Nov 29 '22 at 16:04
53

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:

…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>
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • 2
    +1 for the requirement of junit-vintage to run legacy Junit 3 / 4 tests - had upgraded surefire and junit yet my tests were not getting executed . introducing junit-vintage dependency did the trick – akila May 31 '19 at 06:04
  • adding the surefire plugin is the *main* thing. The junit dependencies don't do the job by themselves. – foo Sep 24 '22 at 19:02
  • @foo Good point. I enhanced that section. – Basil Bourque Sep 24 '22 at 23:27
12
  • 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>

Tristan Elliott
  • 594
  • 10
  • 8
2

For me (all tests are using Junit 5) , it was enough to just use version 3.0.0-M7 for surefire plugin in the plugins section. No need to add surefire plugin to dependencies and i only had org.junit.jupiter.junit-jupiter in my dependencies, no other jupiter dependencies.

Cinders
  • 41
  • 3
2

According to the Junit 5 documentation, this is what we need to execute the tests by using mvn test:

<!-- ... -->
<dependencies>
    <!-- ... -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>5.9.2</version>
        <scope>test</scope>
    </dependency>
    <!-- ... -->
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M7</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>3.0.0-M7</version>
        </plugin>
    </plugins>
</build>
<!-- ... -->

Sources:

Julian Espinel
  • 2,586
  • 5
  • 26
  • 20
1

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>
Milicko
  • 11
  • 1
  • Thanks mate! -- I was tearing my hair out over this :( --- I can't believe they have **re-introduced** the **stupid** JUnit naming-convention from ages back :((( -- how ridiculous! :((( – Rop Nov 01 '22 at 19:57
  • So, to avoid this bugger, we should always add this now: **/*.java – Rop Nov 01 '22 at 20:00
1

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>
Codigo Morsa
  • 820
  • 7
  • 14
0

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>
VinjaNinja
  • 59
  • 7
0

For me not ran tests were due to surefire plugin not being ran at all. This was due to 2 reasons.

  1. I declared sunfire plugin in

    build->pluginmanagement->plugins

    instead of just

    build->plugins

  2. My type was pom instead of jar.

I know these might sound like errors done almost on purpose but it was a result of novice maven user creating pom.xml via copy paste from different projects. I also know that my symptoms were different from what author described (plugin not ran at all instead of no tests found) but I believe it fits well to the thread topic and this solution might help some people.

Marcin K.
  • 683
  • 1
  • 9
  • 20