- Published on
Getting JUnit 5 Working with Surefire
- Authors
- Name
- Yair Mark
- @yairmark
Today I was working on a new Maven module and wanted to use JUnit 5 for my tests. As I had no JUnit 4 legacy I did not have to work about all the JUnit vintage dependencies. So I had my pom setup as follows:
<properties>
<kotlin.version>1.2.41</kotlin.version>
<jackson.version>2.9.5</jackson.version>
<junit.jupiter.version>5.1.0</junit.jupiter.version>
<junit.vintage.version>5.1.0</junit.vintage.version>
<junit.platform.version>1.1.0</junit.platform.version>
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
</properties>
<!-- ... -->
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<compilerPlugins>
<plugin>spring</plugin>
</compilerPlugins>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<executions>
<execution>
<id>default-test</id>
<configuration>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
</configuration>
</execution>
</executions>
<configuration>
<runOrder>alphabetical</runOrder>
</configuration>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>${junit.platform.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
But when I ran Maven I saw that 0 tests were run, failing or in error so clearly something was not configured properly. I came across this answer which got me in the right direction. I ran mvn -X clean test
which output something similar to: Adding managed dependencies for unknown:surefire-testing
. I then saw that there was one dependency in the surefire dependency section which was aimed more at vintage JUnit APIs which I intentionally left out as I thought it was unnecessary. I added it and voila it worked fine. I ended up with:
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>${junit.vintage.version}</version>
</dependency>
<properties>
<kotlin.version>1.2.41</kotlin.version>
<jackson.version>2.9.5</jackson.version>
<junit.jupiter.version>5.1.0</junit.jupiter.version>
<junit.vintage.version>5.1.0</junit.vintage.version>
<junit.platform.version>1.1.0</junit.platform.version>
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
</properties>
<!-- ... -->
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<compilerPlugins>
<plugin>spring</plugin>
</compilerPlugins>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<executions>
<execution>
<id>default-test</id>
<configuration>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
</configuration>
</execution>
</executions>
<configuration>
<runOrder>alphabetical</runOrder>
</configuration>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>${junit.platform.version}</version>
</dependency>
<!-- I had to add the below to get it to work -->
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>${junit.vintage.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>