Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 541
mogoli Avatar asked Nov 22 '18 15:11

mogoli


People also ask

How do I run a JUnit 5 test in Maven?

We can run our unit tests with Maven by using the command: mvn clean test. When we run this command at command prompt, we should see that the Maven Surefire Plugin runs our unit tests. We can now create a Maven project that compiles and runs unit tests which use JUnit 5.

How do I run a Maven JUnit test in Eclipse?

you can run Junit 4 with Maven. You just need the Junit 4 dependency in your pom. You also need the surefire plugin to execute the tests. Hint: By default surefire looks for files with *Test.

Does Maven support JUnit?

The JUnit Platform Provider supports the test JVM system property supported by the Maven Surefire Plugin.


3 Answers

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.

like image 74
LaurentG Avatar answered Oct 12 '22 16:10

LaurentG


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>
like image 30
Basil Bourque Avatar answered Oct 12 '22 14:10

Basil Bourque


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

like image 2
Tristan Elliott Avatar answered Oct 12 '22 15:10

Tristan Elliott