Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

In this tutorial, we’re going to run tests on the new JUnit 5 platform with the Gradle build tool. 

We’ll configure a project that supports both the old and the new version.

Feel free to read A Guide to JUnit 5 for more information about the new version. Or the Introduction to Gradle for in-depth information about the build tool.

2. Gradle Setup

First, we verify if version 4.6 or higher of the build tool is installed since that is the earliest version that works with JUnit 5.

The simplest way is to just run the gradle -v command:

$> gradle -v
------------------------------------------------------------
Gradle 4.10.2
------------------------------------------------------------

And, if necessary, we can follow the installation steps to get the right version.

Once we’ve installed everything, we then need to configure Gradle by using the build.gradle file.

We can start by supplying the unit test platform to the build tool:

test {
    useJUnitPlatform()
}

Now that we’ve specified the platform, we need to supply the JUnit dependencies. This is where we see a noteworthy difference between JUnit 5 and earlier versions.

See, with earlier versions, we only needed one dependency. In JUnit 5, though, the API is separated from the runtime, meaning two dependencies.

The API is manifest with junit-jupiter-api. The runtime is junit-jupiter-engine for JUnit 5, and junit-vintage-engine for JUnit 3 or 4.

We’ll supply these two in testImplementation and timeRuntimeOnly, respectively:

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}

3. Creating Tests

Let’s write our first test. It looks just like earlier versions:

@Test
public void testAdd() {
    assertEquals(42, Integer.sum(19, 23));
}

Now, we can run the test by executing the gradle clean test command.

To verify that we’re using JUnit 5 we can look at the imports. The imports for @Test and assertEquals should have a package starting with org.junit.jupiter.api:

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

So in the last example, we created a test with ‘old’ functionality that has been working for years. We’ll now create another example which uses some of the new functionality in JUnit 5:

@Test
public void testDivide() {
    assertThrows(ArithmeticException.class, () -> {
        Integer.divideUnsigned(42, 0);
    });
}

assertThrows is a new assertion in JUnit5 that replaces the old style of @Test(expected=ArithmeticException.class).

4. Configuring JUnit 5 Tests With Gradle

Next, we’ll explore some deeper integration between Gradle and JUnit5.

Let’s say that we have two types of tests in our suite: long-running and short-running. We could use the JUnit 5 @Tag annotation:

public class CalculatorJUnit5Test {
    @Tag("slow")
    @Test
    public void testAddMaxInteger() {
        assertEquals(2147483646, Integer.sum(2147183646, 300000));
    }
 
    @Tag("fast")
    @Test
    public void testDivide() {
        assertThrows(ArithmeticException.class, () -> {
            Integer.divideUnsigned(42, 0);
        });
    }
}

Then, we tell the build tool which ones to execute. In our case, let’s just execute the short-running (fast) tests:

test {
    useJUnitPlatform {
    	includeTags 'fast'
        excludeTags 'slow'
    }
}

5. Enabling Support for Old Versions

Now, it’s still possible to create JUnit 3 and 4 tests with the new Jupiter engine. Even more, we can mix them with the new version in the same project, say, in a migration scenario.

To start we add some dependencies to the existing build configuration:

testCompileOnly 'junit:junit:4.12' 
testRuntimeOnly 'org.junit.vintage:junit-vintage-engine:5.8.1'

Note how our project now has both junit-jupiter-engine as well as junit-vintage-engine.

Now we create a new class and copy paste the testDivide method we created earlier. Then, we add the imports for @Test and assertEquals. However, this time we make sure to use the old version 4 packages starting which org.junit:

import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class CalculatorJUnit4Test {
    @Test
    public void testAdd() {
        assertEquals(42, Integer.sum(19, 23));
    }
}

6. Conclusion

In this tutorial, we integrated Gradle with JUnit 5. Even more, we also added support for versions 3 and 4.

We’ve seen that the build tool provides excellent support for the old and new versions. Hence we can use the new features in an existing project without the need to change all our existing tests.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW