Upgrading from a Spring boot application with JUnit 4 to JUnit 5, Jupiter

I realised this is only doable with spring 5

To migrate from JUnit 4 to JUnit 5 you can replace @RunWith(SpringRunner.class) with @ExtendWith(SpringExtension.class).

Unfortunately, spring-boot version 1.5.9-RELEASE is based on Spring 4 and the SpringExtension is only available since Spring 5.

Source: https://stackoverflow.com/questions/48019430/junit5-with-spring-boot-1-5

and  http://www.baeldung.com/junit-5-runwith

 

Current dependency:

Exclude the transitive Junit 4 dependency from the Spring boot test dependency

Current:

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

After:

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

 

This will break all your imports with of junit in your test classes. And your vigilant IDE should be compalining already about these.

import org.junit.Test;
import org.junit.runner.RunWith;

Down also goes your tags:

@RunWith(SpringRunner.class)
@Test

You should now be able to use your IDE assist features to generate add teh depency to your pom like show in the following image for intellij.  Or simple copy the depency from the pom snippet below.

add_junit5_to_classpath

 

Snippet here of dependecny

 

Make things easy and do a global find/replace:

import org.junit.Test; ->  import org.junit.jupiter.api.Test;

import org.junit.runner.RunWith; –> import org.junit.jupiter.api.extension.ExtendWith;

import org.springframework.test.context.junit4.SpringRunner; –>

@RunWith(SpringRunner.class) –> @ExtendWith(SpringExtension.class)

 

 

 

 

 

Leave a Reply