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.