DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Frameworks
  4. Spring-Based Apps: Migrating to JUnit 5

Spring-Based Apps: Migrating to JUnit 5

With JUnit 5 newly available, we take a look at how migrating your JUnit 4 tests to JUnit 5 might go as well as a couple of pitfalls to avoid.

Biju Kunjummen user avatar by
Biju Kunjummen
·
Jan. 08, 18 · Tutorial
Like (4)
Save
Tweet
Share
26.55K Views

Join the DZone community and get the full member experience.

Join For Free

This is a quick write-up on migrating a Gradle-based Spring Boot app from JUnit 4 to the shiny new Junit 5. JUnit 4 tests continue to work with Junit 5 Test Engine abstraction, which provides support for tests written in different programming models. In this instance, JUnit 5 supports a Vintage Test Engine with the ability to run JUnit 4 tests.

Here is a sample project with JUnit 5 integrations already in place along with sample tests in JUnit 4 and JUnit 5.

Sample JUnit 4 Candidate Test

As a candidate project, I have a Spring Boot 2 app with tests written in Kotlin using JUnit 4 as the testing framework. This is how a sample test looks with all dependencies explicitly called out. It uses JUnit4's @RunWith annotation to load up the Spring Context:

import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest
import org.springframework.test.context.junit4.SpringRunner
import org.springframework.test.web.reactive.server.WebTestClient
import java.nio.charset.StandardCharsets

@RunWith(SpringRunner::class)
@WebFluxTest(controllers = arrayOf(RouteConfig::class))
class SampleJunit4Test {

    @Autowired
    lateinit var webTestClient: WebTestClient

    @Test
    fun `get of hello URI should return Hello World!`() {
        webTestClient.get()
                .uri("/hello")
                .exchange()
                .expectStatus().isOk
                .expectBody()
                .consumeWith({ m ->
                    assertThat(String(m.responseBodyContent, StandardCharsets.UTF_8)).isEqualTo("Hello World!")
                })

    }

}


The JUnit 4 dependencies are pulled in transitively via the "spring-boot-starter-test" module:

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


Junit 5 Migration

The first step is to pull in the JUnit 5 dependencies along with the Gradle plugin, which enables running the tests.

Plugin:

buildscript {
    dependencies {
        ....
        classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.2'
    }
}
apply plugin: 'org.junit.platform.gradle.plugin'


Dependencies:

testCompile("org.junit.jupiter:junit-jupiter-api")
testRuntime("org.junit.jupiter:junit-jupiter-engine")
testRuntime("org.junit.vintage:junit-vintage-engine:4.12.2")


With these changes in place, all the JUnit 4 tests will continue to run both in the IDE and when the Gradle build is executed. At that point, the tests can be slowly migrated over.

The test I had shown before looks like this with JUnit 5 Jupiter, which provides the programming model for the tests:

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest
import org.springframework.test.context.junit.jupiter.SpringExtension
import org.springframework.test.web.reactive.server.WebTestClient
import java.nio.charset.StandardCharsets

@ExtendWith(SpringExtension::class)
@WebFluxTest(controllers = arrayOf(RouteConfig::class))
class SampleJunit5Test {

    @Autowired
    lateinit var webTestClient: WebTestClient

    @Test
    fun `get of hello URI should return Hello World!`() {
        webTestClient.get()
                .uri("/hello")
                .exchange()
                .expectStatus().isOk
                .expectBody()
                .consumeWith({ m ->
                    assertEquals("Hello World!", String(m.responseBodyContent, StandardCharsets.UTF_8))
                })
    }

}


Note that now, instead of using JUnit 4 the @RunWith annotation, I am using the @ExtendWith annotation and providing SpringExtension as a parameter. It is responsible for loading up the Spring Context like before. The rest of the Spring annotations will continue to work with JUnit 5. This way, tests can be slowly moved over from JUnit 4 to JUnit 5.

Caveats

Not everything is smooth though. There are a few issues in migrating from JUnit 4 to JUnit 5. The biggest of them is likely the support for JUnit's @Rule and @ClassRule annotations. The JUnit 5 documentation does go into the details of how that can be mitigated.

JUnit app Spring Framework Testing

Published at DZone with permission of Biju Kunjummen, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What Was the Question Again, ChatGPT?
  • Top 12 Technical Skills Every Software Tester Must Have
  • How and Why You Should Start Automating DevOps
  • Java Development Trends 2023

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: