Last weekend, a new version of the Apache Maven Surefire-Plugin has been released:
Kudos to @BenediktRitter and @tibor17 for making native #JUnit5 support in @ASFMavenProject Surefire 2.22.0 happen!
Updated the #JUnit Maven samples via https://t.co/IOxKdS57Hk
The provider hosted by the @junitteam will be deprecated soon.
— Christian Stein (@sormuras) June 16, 2018
The Failsafe-Plugin has been updated as well. Both support JUnit 5 natively.
To make use of JUnit 5 in a Spring Boot 2 application, there’s not much todo. Here’s a gist of a POM that brings everything. See comments in the code. Basically all you have to do is overwrite the managed versions of the Surefire- and Failsafe-Plugins and then exclude the JUnit 4 dependency from Spring Boots Starter Test (and all other test related starters, i.e. security-starter-test). You’ll than declare both the JUnit 5 Jupiter Api and Engine, both in scope test. You could put the engine into the plugins dependency, but I couldn’t think of an aspect that’s improved by more cruft. Then, write unit and integration tests as shown (the later with annotated with @ExtendWith(SpringExtension.class)
).
<?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>ac.simons</groupId> | |
<artifactId>junit5demo</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<packaging>jar</packaging> | |
<name>junit5demo</name> | |
<description>Demo project for Spring Boot</description> | |
<parent> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-parent</artifactId> | |
<version>2.0.3.RELEASE</version> | |
<relativePath/> | |
</parent> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | |
<java.version>10</java.version> | |
<!-- Use most recent failsafe and surefire plugin versions --> | |
<maven-failsafe-plugin.version>2.22.0</maven-failsafe-plugin.version> | |
<maven-surefire-plugin.version>2.22.0</maven-surefire-plugin.version> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-web</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-test</artifactId> | |
<scope>test</scope> | |
<!-- Exclude JUnit 4 from starter-test (and all other related test-starter, i.e | |
those for security and project reactor --> | |
<exclusions> | |
<exclusion> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
</exclusion> | |
</exclusions> | |
</dependency> | |
<!-- Provide JUnit 5 API --> | |
<dependency> | |
<groupId>org.junit.jupiter</groupId> | |
<artifactId>junit-jupiter-api</artifactId> | |
<scope>test</scope> | |
</dependency> | |
<!-- and the engine for surefire and failsafe --> | |
<dependency> | |
<groupId>org.junit.jupiter</groupId> | |
<artifactId>junit-jupiter-engine</artifactId> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-maven-plugin</artifactId> | |
</plugin> | |
<!-- Spring Boot configures surefire by default, but not failsafe --> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-failsafe-plugin</artifactId> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
// A unit test | |
package ac.simons.junit5demo; | |
import static org.junit.jupiter.api.Assertions.*; | |
import org.junit.jupiter.api.DisplayName; | |
import org.junit.jupiter.api.Test; | |
class ExampleServiceTest { | |
@Test | |
@DisplayName("Example Service should work!") | |
void exampleServiceShouldWork() { | |
var exampleService = new ExampleService(); | |
assertEquals("Hello, JUnit 5\n", exampleService.getGreeting()); | |
} | |
} |
// An integration test | |
package ac.simons.junit5demo; | |
import org.junit.jupiter.api.Test; | |
import org.junit.jupiter.api.extension.ExtendWith; | |
import org.springframework.boot.test.context.SpringBootTest; | |
import org.springframework.test.context.junit.jupiter.SpringExtension; | |
@ExtendWith(SpringExtension.class) | |
@SpringBootTest | |
class Junit5demoApplicationIT { | |
@Test | |
void contextLoads() { | |
} | |
} |
You’ll notice that only the Failsafe-Plugin has been declared. Spring Boots parent POM already takes care of the Surefire-Plugin.
And that’s all you need for Spring Boot 2 with JUnit 5.
Did you like this article? You can invite me on a coffee if you like.
One comment
@DisplayName(“Example Service should work!”)

void exampleServiceShouldWork() {
I would prefer Spock definetely
4 Trackbacks/Pingbacks
[…] >> Maven: Use JUnit 5 with Spring Boot for Unit and Integration Tests [info.michael-simons.eu] […]
[…] Maven: Use JUnit 5 with Spring Boot for unit and integration tests is a practical blog post that helps you run your unit and integration tests with Maven when you are using Spring Boot. […]
[…] I was following this tutorial but was required to add a few things to my POM because Intellij wasn’t picking up the Jupiter runner correctly: https://info.michael-simons.eu/2018/06/18/maven-use-junit-5-with-spring-boot-for-unit-and-integratio… […]
[…] https://info.michael-simons.eu/2018/06/18/maven-use-junit-5-with-spring-boot-for-unit-and-integratio… as a starting point I recommend to add the jupiter-engine as an explicit […]
Post a Comment