Are you tired of writing tests which have a lot of boilerplate code? If so, get started with Spock Framework >>

JUnit 5 Tutorial: Running Unit Tests With Gradle

This blog post describes how we can create a Gradle project that can compile and run unit tests which use JUnit 5. After we have finished this blog post, we:

  • Can get the required dependencies with Gradle
  • Understand how we can use the Gradle’s native JUnit 5 support.
  • Can run our unit tests with Gradle.

Let’s start by creating a Java project.

If you are using Gradle 4.5 (or older), you cannot use the native JUnit 5 support because it was provided by Gradle 4.6. You have to use the deprecated JUnit 5 Gradle plugin. If you need more information about this plugin, you should read the User Guide of JUnit 5.1.

Creating a Java Project

We can create a Java project by applying the Java plugin. After we have applied this plugin, the source code of our build.gradle file looks as follows:

apply plugin: 'java'

Next, we will find out how we can get the required dependencies with Gradle.

Getting the Required Dependencies

We can get the required dependencies by following these steps:

  1. Ensure that Gradle uses the Maven central repository when it resolves the dependencies of our Gradle build.
  2. Add the junit-jupiter-api (version 5.4.0) dependency to the testCompile dependency configuration. This dependency provides the public API for writing tests and extensions.
  3. Add the used test engine implementations to the testRuntime dependency configuration. Because we want to run only unit tests which use JUnit 5, we have to add the junit-jupiter-engine (version 5.4.0) dependency to the testRuntime dependency configuration.

After we have declared the required dependencies, our build.gradle file looks as follows:

apply plugin: 'java'

repositories {
    mavenCentral()
}

dependencies {
    testCompile('org.junit.jupiter:junit-jupiter-api:5.4.0')
    testRuntime('org.junit.jupiter:junit-jupiter-engine:5.4.0')
}

If we want to run tests which use JUnit 4 or 3, we have to add the junit-vintage-engine dependency (version 5.4.0) to the testRuntime dependency configuration. Also, we have to add the correct JUnit dependency to the testCompile dependency configuration.

For example, if we want to run tests which use both JUnit 4 and 5, our build.gradle file looks as follows:

apply plugin: 'java'

repositories {
    mavenCentral()
}

dependencies {
    testCompile(
            'junit:junit:4.12',
            'org.junit.jupiter:junit-jupiter-api:5.4.0'
    )
    testRuntime(
            'org.junit.jupiter:junit-jupiter-engine:5.4.0',
            'org.junit.vintage:junit-vintage-engine:5.4.0'
    )
}

Let’s move on and find out how we can enable the Gradle’s native JUnit 5 support.

Enabling the Gradle’s Native JUnit 5 Support

Even though Gradle 4.6 (and obviously all newer versions) has a native support for JUnit 5, this support is not enabled by default. If we want to enable it, we have to ensure that the test task uses JUnit 5 instead of JUnit 4.

After we have enabled the Gradle’s native JUnit 5 support, the source code of our build.gradle file looks as follows:

apply plugin: 'java'

repositories {
    mavenCentral()
}

dependencies {
    testCompile(
            'junit:junit:4.12',
            'org.junit.jupiter:junit-jupiter-api:5.4.0'
    )
    testRuntime(
            'org.junit.jupiter:junit-jupiter-engine:5.4.0',
            'org.junit.vintage:junit-vintage-engine:5.4.0'
    )
}

test {
    useJUnitPlatform()
}

We have now created a Gradle project that can run unit tests which use JUnit 5. Next, we will write a simple unit test with JUnit 5.

Writing a Simple Test Class

Before we can write unit tests which use JUnit 5, we have to know these two things:

  • The src/test/java directory contains the source code of our unit tests.
  • The src/test/resources directory contains the resources of our unit tests.

Let’s create a new test class and add one test method to created class. This test method simply writes a message to System.out. The source code of our test class looks as follows:

import org.junit.jupiter.api.Test;

class JUnit5ExampleTest {

    @Test
    void justAnExample() {
        System.out.println("This test method should be run");
    }
}
If you want to get more information about JUnit 5 test classes, you should read this blog post. Also, we shouldn’t write tests which write messages to System.out. I use this technique here only because it is an easy way to verify that our test method is run.

Let’s move on and find out how we can run our unit tests.

Running Unit Tests With Gradle

We can run our unit tests with Gradle by using the following command: gradle clean test. When we run this command at command prompt, we see that Gradle runs our unit tests:

$ gradle clean test

> Task :test

net.petrikainulainen.junit5.JUnit5ExampleTest > justAnExample() STANDARD_OUT
This test method should be run
By default, the test task doesn’t show the information written to System.out or System.err. If we want to show this information, we can make the required changes to the configuration of the test task by following the instructions given in the blog post: Show Standard Out or Error Output from Tests.

We can now create a Gradle project that compiles and runs unit tests which use JUnit 5. Also, we know how we can run our unit tests with Gradle. Let’s summarize what we learned from this blog post.

Summary

This blog post has taught us five things:

  • The junit-jupiter-api dependency allows us to write tests and extensions which use JUnit 5.
  • The junit-jupiter-engine dependency allows us to run tests which use JUnit 5.
  • The junit-vintage-engine dependency allows us to run tests which use JUnit 3 or 4.
  • Gradle has a native support for JUnit 5, but this support isn’t enabled by default.
  • We can run our unit tests by using the command: gradle clean test.

P.S. You can get the example application of this blog post from Github.

12 comments… add one
  • Hi Petri,

    thanks for this great article. I just try to get different test source sets used by the junit5-gradle-plugin, but unforunately tests in a new source set integration-tests won’t get executed.
    Do you plan to provide an update of the setup for Gradle using JUnit 5? Maybe as an update of your article “getting-started-with-gradle-integration-testing”?
    Thanks in advance
    Stefan

    Reply
    • This Stack Overflow article seems to go in the right direction, but I didn’t get it running …
      https://stackoverflow.com/questions/45297339/junit-5-under-gradle-with-multiple-source-sets
      Stefan

      Reply
      • No it works:

        task integrationTest(
        type: JavaExec,
        description: ‘Runs the integration tests.’,
        group: ‘Verification’
        ) {
        dependsOn testClasses
        mustRunAfter junitPlatformTest

        classpath = sourceSets.integrationTest.runtimeClasspath
        main = ‘org.junit.platform.console.ConsoleLauncher’
        args = [‘–scan-class-path’,
        sourceSets.integrationTest.output.getClassesDirs().asPath,
        ‘–reports-dir’, “${buildDir}/test-results/junit-e2e”]
        }
        check.dependsOn integrationTest

        With dependency

        testRuntime(“org.junit.platform:junit-platform-console:$junitPlatformVersion”)

        Stefan

        Reply
        • Hi Stefan,

          First, it’s good to hear that you were able to solve your problem while I was working. Also, thank you for posting your solution here. I am going to write a blog post about this in the future. This post will probably be a part of my JUnit 5 tutorial, but I have to also admit that I will probably wait until Gradle releases the official JUnit 5 support because I think that it will make things a bit easier.

          Reply
  • That was very helpful. thank you :)

    Reply
    • You are welcome!

      Reply
  • This is no longer needed. Gradle 4.6 supports JUnit5 natively, with:

    
    test { useJunitPlatform() }
    
    
    Reply
    • Good catch! I will add a note about this to the beginning of this blog post. I will update this blog post eventually, but I hope that the note will help people to avoid any extra work. By the way, thank you for reminding me about this.

      Reply
  • Thanks for very great article. It helped me a lot!

    Reply
    • You are welcome. I am happy to hear that this blog post was useful to you.

      Reply

Leave a Comment