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)).

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

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

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 *