Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

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 complete code example is available in the GitHub project. Feel free to use it as a starting point for your own project.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.