Running Unit Tests With Gradle

Understand why we should categorize our tests by using JUnit 4 categories.

Can run unit tests that use JUnit 4 Categories

Watch the Lesson

The text version of this lesson is given in the following:

Running Unit Tests With Gradle

If we want to create a Java project with Gradle, we have to apply the Gradle Java plugin. We can do this by adding the following line into our build.gradle file:

apply plugin: 'java'

If we use the default configuration, Gradle runs all tests that are found from the test classes which are found from the src/test/java directory. Because we are using JUnit, a test class is a class that fulfills at least one of the following conditions:

The class or its super class extends the TestCase or GroovyTestCase class.

The class or its super class is annotated with the @RunWith annotation.

The class or its super class contain a method that is annotated with the @Test annotation.

If our unit tests have resources such as property files or logging configuration files, we must put these resources to the src/test/resources directory.

By default, Gradle doesn’t print the information that is written to System.out or System.err. We can enable this feature by following the instructions given in this blog post.

If we write only unit tests, we don’t have to make any more changes to our build script. However, the odds are that we have to write also integration and end-to-end tests.

Let’s find out why we should use JUnit 4 categories and how we can use them.

Configuring the Category of Our Unit Tests

We don’t have to use JUnit 4 categories because Gradle can differentiate unit, integration, and end-to-end tests. However, I will describe how we can use JUnit 4 categories with Gradle because JUnit 4 categories give us the the possibility to create several subcategories inside one parent category and configure the tests of these subcategories on class or method level.

For example, we can create two different categories for our integration tests. The tests that belong the category one are run in the local development environment, and the tests that belong to the category two are run in the CI server. This is useful if some of our integration tests are so slow that we don’t want to run them every time when we run our integration tests.

Let’s start by creating a new test class. The CategoryTest class contains one unit test that writes the string: ‘The category: UnitTest’ to System.out. Also, because we want to configure the category of this test class, we have to annotate it with the @Category annotation.

The UnitTest interface is a marker interface which is used to identify our unit tests. Its source code looks as follows:

public interface UnitTest {
}

After we have created our test class and configured its category, we have to ensure that Gradle runs all unit tests that belong to the UnitTest category. We can make the required changes to configuration of the test task by following these steps:

Enable JUnit support. Even though JUnit is enabled by default, we have to do this because we want to make changes to the JUnit configuration.

Set the fully qualified class name of the UnitTest interface as the value of the includeCategories JUnit configuration option.

We can finish these steps by adding the following lines into our build.gradle file: