I released five new sample lessons from my Test With Spring course: Introduction to Spock Framework

JUnit 5 Tutorial: Running Unit Tests With Gradle

This blog post describes how we can get the required dependencies with Gradle, explains how we can configure the JUnit 5 Gradle plugin, and demonstrates how we can run our unit tests with Gradle.

Let’s start by configuring the JUnit 5 Gradle plugin.

Configuring the JUnit 5 Gradle Plugin

The JUnit 5 Gradle plugin allows us to run tests which are supported by the used test engines. We can configure the JUnit 5 Gradle plugin by following these steps:

First, we have to configure the dependencies of our build script. We can do this by adding the junit-platform-gradle-plugin (version 1.0.0-RC3) dependency to the classpath configuration.

After we have done this, the source code of our build.gradle file looks as follows:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-RC3'
    }
}

Second, we have to apply the JUnit 5 Gradle plugin. After we have applied this plugin, the source code of our build.gradle file looks as follows:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-RC3'
    }
}

apply plugin: 'java'
apply plugin: 'org.junit.platform.gradle.plugin'

After we have configured the JUnit 5 Gradle plugin, we have to get the required dependencies. Let’s find out how we can do it.

Getting the Required Dependencies

We can get the required dependencies by following these steps:

  1. Add the junit-jupiter-api (version 5.0.0-RC3) dependency to the testCompile dependency configuration. This dependency provides the public API for writing tests and extensions.
  2. Add the used test engine implementations to the testRuntime configuration. Because we want to run only unit tests which use JUnit 5, we have to add the junit-jupiter-engine (version 5.0.0-RC3) dependency to the testRuntime dependency configuration.

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

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-RC3'
    }
}

apply plugin: 'java'
apply plugin: 'org.junit.platform.gradle.plugin'

repositories {
    mavenCentral()
}

dependencies {
    testCompile(
            'org.junit.jupiter:junit-jupiter-api:5.0.0-RC3'
    )
    testRuntime(
            'org.junit.jupiter:junit-jupiter-engine:5.0.0-RC3'
    )
}
If we want to run tests which use either JUnit 4 or JUnit 3, we have to add the junit-vintage-engine (version 4.12.0-RC3) dependency to the testRuntime dependency configuration.

If we are using an IDE that is bundled with an older JUnit 5 version, it throws an exception when we try to run our unit tests by using our IDE. We can fix this problem by adding the following dependencies to the testRuntime dependency configuration:

  • The junit-vintage-engine (version 4.12.0-RC3) dependency adds support for unit tests that use JUnit 4 or JUnit 3.
  • The junit-platform-launcher (version 1.0.0-RC3) dependency provides a public API for configuring and launching tests. This API is typically used by IDEs and build tools.
  • The junit-platform-runner (version 1.0.0-RC3) dependency allows us to run tests and test suites in a JUnit 4 environment.

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

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-RC3'
    }
}

apply plugin: 'java'
apply plugin: 'org.junit.platform.gradle.plugin'

repositories {
    mavenCentral()
}

dependencies {
    testCompile(
            'org.junit.jupiter:junit-jupiter-api:5.0.0-RC3'
    )
    testRuntime(
            'org.junit.jupiter:junit-jupiter-engine:5.0.0-RC3',
            'org.junit.vintage:junit-vintage-engine:4.12.0-RC3',
            'org.junit.platform:junit-platform-launcher:1.0.0-RC3',
            'org.junit.platform:junit-platform-runner:1.0.0-RC3'
    )
}

We have now created a Gradle project that can run unit tests which use JUnit 5. Let’s move on and 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");
    }
}
I won’t describe this test class in this blog post because my upcoming blog posts will provide introduction to JUnit 5 test classes.

Also, we shouldn’t write tests which write messages to System.out. I use this technique here only because it is an easy to way see 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 running the following command at command prompt:

gradle clean test

When we run our unit tests, we should see the following output:

$ gradle clean test

> Task :junitPlatformTest
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.

As we can see, Gradle runs our unit test. Let’s summarize what we learned from this blog post.

Summary

This blog post has taught us four 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.
  • The JUnit 5 Gradle plugin allows us to run tests which are supported by the used test engines.

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

About the Author

Petri Kainulainen is passionate about software development and continuous improvement. He is specialized in software development with the Spring Framework and is the author of Spring Data book.

About Petri Kainulainen →

1 comment… add one

    Leave a Comment