How to use JUnit 5 with Gradle

There are several ways to use JUnit 5 with Gradle.

One possibility is to use Gradle ≥ 4.6 which comes with native support for the JUnit platform. Add the following code to your build.gradle to use JUnit 5:

repositories {

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

test {

Test logging

s java9.collections.MapTest > java9.collections.MapTest$Of.shouldThrowAnExceptionWhenPassedNullValue() PASSED

java9.collections.MapTest > java9.collections.MapTest$Of.shouldCreateAMapOfTypeImmutableCollections$Map0() PASSED

To get a test logging similar to the above, add the following testLogging to the test closure:

test {
    testLogging {
        events 'PASSED', 'FAILED', 'SKIPPED'

There are three additional TestLogEvents: STARTED, STANDARD_OUT and STANDARD_ERROR. To add the Standard-Out and Standard-Error streams, you can also add test.testLogging.showStandardStreams = true.

Unfortunately, the @DisplayName annotation is not supported yet for the Gradle test task’s logging, but you will benefit from it when you execute the tests in IntelliJ for example.

Test summary

Test result: SUCCESS
Test summary: 38 tests, 38 succeeded, 0 failed, 0 skipped

To get a test summary like the above, add an afterSuite to the test closure:

test {
    afterSuite { desc, result ->
        if (!desc.parent) {
            println "\nTest result: ${result.resultType}"
            println "Test summary: ${result.testCount} tests, " +
                    "${result.successfulTestCount} succeeded, " +
                    "${result.failedTestCount} failed, " +
                    "${result.skippedTestCount} skipped"

I have published a project on GitHub that uses this configuration.