0
votes

I am attempting to configure a Java Maven project to run JUnit 5 Tests when it is built.

I have the standard file structure for a Java project, with my project containing a src/main/java folder with several files that are built fine, and I recently added a src/test/java folder for JUnit tests.

Within src/test/java, I have a single file that contains a JUnit test, which is as follows:

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class SampleTests {

    @Test
    @DisplayName("This Test Should fail")
    public void testThatAlwaysFails() {
        assertEquals(3, 2);
    }
}

The following is the relevant portion of my pom.xml file:

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    ...
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <junit.jupiter.version>5.6.2</junit.jupiter.version>
    </properties>
    <dependencies>
        ...
        <!-- For JUnit 5-->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <!-- For Running the JUnits-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
            </plugin>
        </plugins>
    </build>
</project>

This all seems to be in line with the documentation I've found as well as the solutions in other SO posts. However, when I go to build the project via mvn clean install, these are the only lines of output related to tests:

[INFO] --- maven-compiler-plugin:3.8.1:testCompile (groovy-tests) @ <project-name-here> ---
[INFO] Changes detected - recompiling the module!
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ <project-name-here> ---
[INFO] 
[INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ <project-name-here> ---

(I have removed the actual project name from the output, but it is there as expected).

What would be the cause of this, and how could I configure my pom file differently so the tests are run? I would expect that right after the maven-surefire-plugin line there is a line of output choosing a provider, and then afterwards it would say which tests it found and the result, but none of this appears in the console. I have tried many of the versions for JUnit Jupiter as well as the Surefire Plugin, and the same thing happens. I believe I got it to work one time using a much older setup for JUnit 5 and the Surefire plugin, but I don't remember the versions and I want it to work using the latest versions of these dependencies if possible.

If it makes a difference, I am also running mvn clean install from an IntelliJ IDEA Maven configuration.

1
There is a dedicated section within the Maven Surefire Plugin page that describes how to setup JUnit 5 with Maven Surfire: maven.apache.org/surefire/maven-surefire-plugin/examples/…rieckpil

1 Answers

0
votes

I found the following tips in the maven documentation, and successfully ran Junit Tests. Here's Running Tests.

In order to use a different JUnit 5 version (e.g., 5.7.0), you may need to include the corresponding versions of the junit-platform-launcher, junit-jupiter-engine, and junit-vintage-engine JARs in the classpath.

Additional Maven Dependencies

<!-- Only needed to run tests in a version of IntelliJ IDEA that bundles older versions -->
<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-launcher</artifactId>
    <version>1.7.0</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.7.0</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    <version>5.7.0</version>
    <scope>test</scope>
</dependency>

After adding the above jar file, maven test successfully runs the test code