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.
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:
1plugins {
2 id 'java'
3}
Additional Reading:
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:
First, ensure that Gradle uses the Maven central repository when it resolves the dependencies of our Gradle build. After we have configured the used Maven repository, our build.gradle file looks as follows:
1apply plugin: 'java'
2
3repositories {
4 mavenCentral()
5}
Second, add the junit-jupiter (version 5.8.2) dependency to the testImplementation dependency configuration. This is an aggregator artifact which simplifies the dependency management because it has the following transitive dependencies:
- The
junit-jupiter-apidependency (testImplementationdependency configuration) provides the public API for writing tests and extensions. - The
junit-jupiter-paramsdependency (testImplementationdependency configuration) provides support for writing parameterized tests. - The
junit-jupiter-enginedependency (testRuntimeClasspathdependency configuration) contains the implementation of the JUnit Jupiter test engine that runs our unit tests. If we add this dependency to our classpath, we can run our tests by leveraging the Gradle’s native JUnit 5 support if we are using Gradle 4.6 or newer.
After we have declared the required dependencies, our build.gradle file looks as follows:
1apply plugin: 'java'
2
3repositories {
4 mavenCentral()
5}
6
7dependencies {
8 testImplementation('org.junit.jupiter:junit-jupiter:5.8.2')
9}
testImplementation dependency configuration is a new dependency configuration that replaced the testCompile dependency configuration. If you want to get more information about this dependency configuration, you should take a look at this StackOverflow answer.
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 (version 4.6 or newer) has a native support for JUnit 5, this support isn’t 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:
1plugins {
2 id 'java'
3}
4
5repositories {
6 mavenCentral()
7}
8
9dependencies {
10 testImplementation('org.junit.jupiter:junit-jupiter:5.8.2')
11}
12
13test {
14 useJUnitPlatform()
15}
Additional Reading:
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 the created class. This test method simply writes a message to System.out. The source code of our test class looks as follows:
1import org.junit.jupiter.api.Test;
2
3class JUnit5ExampleTest {
4
5 @Test
6 void justAnExample() {
7 System.out.println("This test method should be run");
8 }
9}
System.out. I use this technique here only because it’s 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: 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-apidependency provides the public API that allows us to write tests and extensions which use JUnit 5. - The
junit-jupiter-enginedependency allows us to run tests which use JUnit 5 with Gradle. - The
junit-jupiterdependency is an aggregator artifact which simplifies the dependency management because it ensures that the required dependencies are found from the classpath. - 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.