Migrating Spring Boot tests from Junit 4 to Junit 5

Upasana | July 10, 2020 | 3 min read | 7,067 views | java junit


In this tutorial we will learn how to migrate existing Junit 4 testcases to Junit 5 with Spring Boot 2.3.0
Table of Contents
  1. Dependencies update in Spring Boot 2

  2. Annotations changes in Junit 5 - @Test, @Disabled

Important note

Spring Boot 2.3.0 includes junit5 as the default library for unit testing. It also includes junit vintage library that allows you to run Junit 4 and Junit 5 in parallel, so that you can slowly and easily migrate existing testcases from Junit 4 to Junit 5 without breaking anything.

Dependencies update

Spring Boot starter for test includes dependencies for junit framework too, but the version of Junit framework depends on the Spring Boot version itself.

until Spring Boot 2.1.9.RELEASE, gradle dependencies looked like below:

plugins {
    id "java"
    id 'org.springframework.boot' version "2.1.9.RELEASE"
    id "io.spring.dependency-management" version "1.0.8.RELEASE"
}

testImplementation('org.springframework.boot:spring-boot-starter-test')

revised dependencies with Spring Boot 2.3.0.RELEASE are:

plugins {
    id "java"
    id 'org.springframework.boot' version "2.3.0.RELEASE"
    id "io.spring.dependency-management" version "1.0.9.RELEASE"
}

testImplementation('org.springframework.boot:spring-boot-starter-test') {
    exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'  (1)
}

test {
    useJUnitPlatform()
}
1 junit-vintage-engine is included by default to allows easy migration from Junit 4 to Junit 5, by allowing both Junit 4 and Junit 5 based tests to run in parallel. You can exclude junit-vintage-engine if you do not have any Junit 4 based testcase in your application.

Before Spring Boot 2.2.6.RELEASE, spring-boot-starter-test included junit4 dependency transitively. Spring Boot 2.3.0 onwards, Junit Jupiter is included instead.

Annotation changes in Junit 5

We had @Ignore annotation till Junit 4 for disabling certain testcases inside a class.

Ignoring testcase

Ignore test in junit 4

@Test
@Ignore
public void testMethod() {

}

In Junit 5 equivalent way to ignore/disable a testcase is to use @Disabled annotation:

@Test
@Disabled("disabled until bug#1039 is fixed")
public void contextLoads() {

}

We can specify the reason why a particular testcase was disabled.

RunWith configuration

Junit 4 based Integration test with Spring boot looks like below:

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

    @Test
    public void contextLoads() {

    }
}

Junit 5 equivalent is:

@ExtendWith(SpringExtension.class)
@SpringBootTest
public class ApplicationTests {

    @Test
    public void contextLoads() {

    }
}

Infact with the latest version of Spring Boot 2.3.0, you do not even need to specify @ExtendWith, as @SpringBootTest will take care of it behind the scenes.

@SpringBootTest
public class ApplicationTests {

    @Test
    public void contextLoads() {

    }
}

Here is the definition for @SpringBootTest:

SpringBootTest.java
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@BootstrapWith(SpringBootTestContextBootstrapper.class)
@ExtendWith(SpringExtension.class)
public @interface SpringBootTest {

}

Use Tag instead of Category

@Category no longer exists, we shall use @Tag instead

@Test
@Tags(Tag("security"), Tag("fast"))
@DisplayName("Intrusive account creation test that is disabled by default")
fun `test account creation and security`() {

}

Run testcase with specific Tag

To filter tests by tags, use the below configuration in build.gradle

build.gradle
test {
    useJUnitPlatform {
        includeTags 'fast', 'security'
//        excludeTags 'slow'
    }
}

This config will only run those testcases that are either tagged with fast or security.

Before/BeforeClass no longer exists

  • @Before and @After no longer exist; use @BeforeEach and @AfterEach instead.

  • @BeforeClass and @AfterClass no longer exist; use @BeforeAll and @AfterAll instead.


Top articles in this category:
  1. JUnit 5 Parameterized Tests
  2. Junit interview questions for SDET automation engineer
  3. Junit 5 Platform Launcher API
  4. Writing a simple Junit 5 test
  5. Creating custom Tag in Junit5 based tests
  6. Rest Assured API Testing Interview Questions
  7. 50 Java Interview Questions for SDET Automation Engineer

Recommended books for interview preparation:

Find more on this topic:
Buy interview books

Java & Microservices interview refresher for experienced developers.