Ali Dehghani Ali Dehghani - 7 months ago 28
Java Question

Surefire is not picking up Junit 5 tests

I wrote a simple test method with JUnit 5:

public class SimlpeTest {
@Test
@DisplayName("Some description")
void methodName() {
// Testing logic for subject under test
}
}


But when I run
mvn test
, I got:

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.alimate.FirstGlanceTest
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec

Results :

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


Somehow, surefire didn't recognize that test class. My
pom.xml
looks like:

<properties>
<java.version>1.8</java.version>
<junit.version>5.0.0-SNAPSHOT</junit.version>
</properties>

<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit5-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<repositories>
<repository>
<id>snapshots-repo</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<updatePolicy>always</updatePolicy>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>


Any idea how to make this work?

Answer

The maven-surefire-plugin, as of today, does not have full support of JUnit 5. There is an open issue about adding this support in SUREFIRE-1206.

As such, you need to use a custom provider. One has already been developed by the JUnit team; from the user guide, you need to add the surefire-junit5 provider:

<plugin>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.19</version>
  <dependencies>
    <dependency>
      <groupId>org.junit</groupId>
      <artifactId>surefire-junit5</artifactId>
      <version>5.0.0-SNAPSHOT</version>
    </dependency>
  </dependencies>
</plugin>

Also, be sure to declare the junit5-api dependency with a scope of test:

<dependency>
    <groupId>org.junit</groupId>
    <artifactId>junit5-api</artifactId>
    <version>${junit.version}</version>
    <scope>test</scope> <!-- only needed during test -->
</dependency>