loading...

How to enable JUnit 5 in new Spring Boot project

martinbelev profile image Martin Belev ・3 min read

This post was originally posted on Medium.

In this article, we will learn how to enable JUnit 5 in a newly created Spring
Boot project. We are going through the following steps:

  1. Initialize new Sprint Boot project
  2. Having a look at our pom.xml and mostly on sprint-boot-starter-test dependency, going a little deeper in spring-boot-starter-test and see what JUnit version it uses
  3. How to exclude child dependency that comes from one of our dependencies using Maven
  4. Add JUnit 5
  5. Migrate JUnit 4 to JUnit 5

1) First, let’s go to Spring Boot initializr and generate a new project.

Sprint Boot initializr default values

The defaults should be fine and you can click the “Generate Project” button. You should have downloaded a .zip archive of the starter Sprint Boot project. Unzip it and open it with IDE of your choice (I am using IntelliJ and the following code samples and examples will be shown from it). After opening it, you should see the following structure:

Sprint Boot generated project structure

2) Now let’s focus on the pom.xml.

In our pom.xml we can see the following dependency which includes libraries (such as JUnit, Hamcrest, and Mockito) for testing Spring Boot applications.

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
</dependency>

We are going a little deeper to see the exact dependencies and their versions focusing on junit with which spring-boot-starter-test comes (in IntelliJ you can do this by Ctrl + click onto spring-boot-starter-test. In the snippet below, we can see that sprint-boot-starter-test comes with JUnit 4.12 but there is JUnit 5 already. So how can we use the newer version of JUnit in our new Spring Boot project?

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.12</version>
  <scope>compile</scope>
</dependency>

3) We should have a way to exclude JUnit 4 because we are currently depending on it because of spring-boot-starter-test. We can do this by adding the following lines to our spring-boot-starter-test dependency by which we exclude JUnit 4.

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>

  <exclusions>
    <exclusion>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
    </exclusion>
  </exclusions>
</dependency>

4) We are now going to configure JUnit 5 as a dependency using Maven. We will add the following dependencies in pom.xml

<dependency>
  <groupId>org.junit.jupiter</groupId>
  <artifactId>junit-jupiter-api</artifactId>
  <version>5.3.2</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.junit.jupiter</groupId>
  <artifactId>junit-jupiter-engine</artifactId>
  <version>5.3.2</version>
  <scope>test</scope>
</dependency>

And we should add the following maven plugin to our build plugins

<plugin>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.22.0</version>
</plugin>

5) We have removed the dependency of JUnit 4, we have added JUnit 5 and now it is time to make a little code changes in order to use JUnit 5. Let’s focus on DemoApplicationTests.java where we can see the following code


package com.example.demo;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {

    @Test
    public void contextLoads() {
    }

}

Actually, the only things that we have to change are the RunWith annotation because it’s from JUnit 4 and the import of Test annotation. After the change, our test should look like

package com.example.demo;

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
public class DemoApplicationTests {

    @Test
    public void contextLoads() {
    }

}

You should be ready to start writing tests using JUnit 5 now. : )

Posted on by:

martinbelev profile

Martin Belev

@martinbelev

Lead Software Developer at The Stars Group, JS/TS/React, former maths competitor, fitness and cooking enthusiast

Discussion

markdown guide
 

great thanks, very simple configuration

 

For my application using spring-boot version 2.1.9.RELEASE adding maven-surefire-plugin was not needed. Also, the junit-jupiter-api and junit-jupiter-engine dependencies have the managed version 5.3.2 already. But I could not figure this out that if spring-boot-starter-test is using JUnit-4 then where does this managed version of JUnit-5 is coming from?

 

spring-boot-starter-test >2.2.0 comes with Junit 5, so no need for this if you use the most recent version of Spring Boot (or of spring-boot-starter-web).