Migrating Spring Boot tests from Junit 4 to Junit 5

Carvia Tech | October 27, 2019 | 2 min read | 2 views | java junit


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

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

Important note

Spring Boot 2.2.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.2.0.RELEASE are:

plugins {
    id "java"
    id 'org.springframework.boot' version "2.2.0.RELEASE"
    id "io.spring.dependency-management" version "1.0.8.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.0.RELEASE, spring-boot-starter-test included junit4 dependency transitively. Spring Boot 2.2.0 onwards, Junit Jupiter is included instead.

Annotation changes in Junit 5

Ignoring testcase

Ignore test in junit 4

@Test
@Ignore
public void testMethod() {

}

Junit 5 equivalent is:

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

}

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() {

    }
}

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. SDET: JUnit interview questions for automation engineer
  2. Top 50 SDET Java Programming Interview Questions & Answers
  3. SDET: Rest Assured Interview Questions
  4. Junit 5 Platform Launcher API
  5. Writing a simple Junit 5 test
  6. Parameterized Tests using JUnit 5
  7. Creating custom Tag in Junit5 based tests



Find more on this topic:
SDET Interviews image
SDET Interviews

SDET Java Interview pattern and collection of questions covering SDET coding challenges, automation testing concepts, functional, api, integration, performance and security testing, junit5, testng, jmeter, selenium and rest assured

Last updated 1 week ago


Recommended books for interview preparation:

This website uses cookies to ensure you get the best experience on our website. more info