Maven: Use JUnit 5 with Spring Boot for unit and integration tests

A quick tip how to configure your Maven based Spring Boot application to use JUnit 5 for unit and integration tests
June 18, 2018 by Michael

Last weekend, a new version of the Apache Maven Surefire-Plugin has been released:

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

  1. gondri wrote:

    @DisplayName(“Example Service should work!”)
    void exampleServiceShouldWork() {
    🙂
    I would prefer Spock definetely

    Posted on June 25, 2018 at 8:37 PM | Permalink
4 Trackbacks/Pingbacks
  1. Java Weekly, Issue 234 | Baeldung on June 22, 2018 at 9:45 AM

    […] >> Maven: Use JUnit 5 with Spring Boot for Unit and Integration Tests [info.michael-simons.eu] […]

  2. Java Testing Weekly 26 / 2018 on June 25, 2018 at 8:36 AM

    […] 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. […]

  3. […] 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&#8230; […]

  4. […] https://info.michael-simons.eu/2018/06/18/maven-use-junit-5-with-spring-boot-for-unit-and-integratio&#8230; as a starting point I recommend to add the jupiter-engine as an explicit […]

Post a Comment

Your email is never published. We need your name and email address only for verifying a legitimate comment. For more information, a copy of your saved data or a request to delete any data under this address, please send a short notice to michael@simons.ac from the address you used to comment on this entry.
By entering and submitting a comment, wether with or without name or email address, you'll agree that all data you have entered including your IP address will be checked and stored for a limited time by Automattic Inc., 60 29th Street #343, San Francisco, CA 94110-4929, USA. only for the purpose of avoiding spam. You can deny further storage of your data by sending an email to support@wordpress.com, with subject “Deletion of Data stored by Akismet”.
Required fields are marked *