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:

Ensure that Gradle uses the Maven central repository when it resolves the dependencies of our Gradle build. Add the junit-jupiter-api (version 5.4.2) dependency to the testImplementation dependency configuration. This dependency provides the public API for writing tests and extensions. 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.2) 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 { testImplementation('org.junit.jupiter:junit-jupiter-api:5.4.2') testRuntime('org.junit.jupiter:junit-jupiter-engine:5.4.2') }

testImplementation dependency configuration is a new dependency configuration that replaces the testCompile dependency configuration. If you want to get more information about this dependency configuration, you should Thedependency configuration is a new dependency configuration that replaces thedependency configuration. If you want to get more information about this dependency configuration, you should take a look at this StackOverflow answer

If we want to run tests which use JUnit 4 or 3, we have to add the junit-vintage-engine dependency (version 5.4.2) to the testRuntime dependency configuration. Also, we have to add the correct JUnit dependency to the testImplementation 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 { testImplementation( 'junit:junit:4.12', 'org.junit.jupiter:junit-jupiter-api:5.4.2' ) testRuntime( 'org.junit.jupiter:junit-jupiter-engine:5.4.2', 'org.junit.vintage:junit-vintage-engine:5.4.2' ) }

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 { testImplementation('org.junit.jupiter:junit-jupiter-api:5.4.2') testRuntime('org.junit.jupiter:junit-jupiter-engine:5.4.2') } 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"); } }

System.out . I use this technique here only because it is an easy way to verify that our test method is 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. 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

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: By default, thetask doesn’t show the information written toor. If we want to show this information, we can make the required changes to the configuration of thetask 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.

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.

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.

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.