Running Unit Tests With Gradle
After we have finished this lesson, we
- Know how we can run 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
orGroovyTestCase
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.
Additional Reading:
Let’s write one unit test that writes the string: ‘The default configuration’ to System.out
. After we have written this unit test, the source code of our test class looks as follows:
import org.junit.Test; public class DefaultTest { @Test public void defaultTest() { System.out.println("The default configuration"); } }
We can now run our unit tests by using the following command at command prompt:
gradle clean test
If we don’t want to clean our build before we run our unit test, we have to use the command:
gradle test
Additional Reading:
When we run our unit tests, we notice that Gradle runs our unit test:
:clean :compileJava UP-TO-DATE :processResources UP-TO-DATE :classes UP-TO-DATE :compileTestJava :processTestResources UP-TO-DATE :testClasses :test com.testwithspring.starter.unittests.DefaultTest > defaultTest STANDARD_OUT The default configuration BUILD SUCCESSFUL Total time: 4.467 secs
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.
The lesson: Introduction to JUnit 4 Test Runners provides more information about JUnit 4 categories.
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 source code of our test class looks as follows:
import org.junit.Test; import org.junit.experimental.categories.Category; @Category(UnitTest.class) public class CategoryTest { @Test public void categoryTest() { System.out.println("The category: UnitTest"); } }
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 theincludeCategories
JUnit configuration option.
We can finish these steps by adding the following lines into our build.gradle file:
test { useJUnit { includeCategories 'com.testwithspring.starter.unittests.UnitTest' } }
Additional Reading:
When we run our unit tests, we notice that Gradle runs only the test found from the CategoryTest
class and ignores the test found from the DefaultTest
class:
:clean :compileJava UP-TO-DATE :processResources UP-TO-DATE :classes UP-TO-DATE :compileTestJava :processTestResources UP-TO-DATE :testClasses :test com.testwithspring.starter.unittests.CategoryTest > categoryTest STANDARD_OUT The category: UnitTest BUILD SUCCESSFUL Total time: 4.532 secs
Let’s summarize what we learned from this lesson.
Summary
This lesson has taught us three things:
- We can run our unit tests by applying the Gradle Java plugin.
- Gradle can differentiate unit, integration, and end-to-end tests.
- We should use JUnit 4 categories because they provide flexible configuration and give us the possibility to run slow tests in the CI server.
- We can specify the used JUnit 4 category by using the
includeCategories
JUnit configuration option.
Previous LessonNext Lesson