Running JUnit Tests with Gradle

Running JUnit tests with Gradle is as simple as executing the gradle test command into the project’s root directory. Gradle has built-in support for running JUnit tests, and we can customize the behavior according to the project’s requirements.

# Run all tests
gradle test

# Clean the test results, then run all tests
gradle cleanTest test

# Clean the test results, run all tests, and show detailed output
gradle cleanTest test -i

# Run a specific test method in a specific test class
gradle test --tests com.howtodoinjava.junit5.examples.AppTest.testCalAdd

# Run all test methods in a specific test class
gradle test --tests com.howtodoinjava.junit5.examples.AppTest

# Run all test methods in all test classes within a package
gradle test --tests com.howtodoinjava.junit5.examples.*

# Run all test methods in test classes whose names start with "Test"
gradle test --tests com.howtodoinjava.junit5.examples.Test*

# Run a specific test method in all test classes within a package
gradle test --tests com.howtodoinjava.junit5.examples.*.testCalSubtract

# Run multiple test classes/methods in separate invocations
gradle test --tests com.howtodoinjava.junit5.examples.AppTest --tests /
com.howtodoinjava.junit5.examples.ordering.CustomOrderTests

See Also: Running Tests with Maven

1. Setting Up JUnit Plugin for Gradle

In the project’s build.gradle file, add the latest version of JUnit dependency to the test implementation dependencies. For JUnit 5, you’ll also need the JUnit Platform dependencies.

test {
    useJUnitPlatform()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.0'
}

For JUnit 4, the dependencies would look slightly different:

dependencies {
    testImplementation 'junit:junit:4.13'
}

2. Executing Unit Tests with Gradle

To execute all tests run the 'gradle test' command.

gradle test

It is important to note that the ‘gradle test‘ command executes the tests only one time per change. So if you are running it the second time there will be no output on test results until you modify the tests. Gradle says UP-TO-DATE on unmodified tests.

If you want to force the test cases to run, use ‘gradle cleanTest test‘.

gradle cleanTest test

3. Customizing Logging Output

Also, if you want to run tests with more verbose logging, you can switch on the INFO logs by adding ‘-i‘ flag to the command.

gradle cleanTest test -i

If you want to capture the test results more concisely, you add a Groovy closure that does the logging for you:

test {
    useJUnitPlatform()

    afterTest { desc, result ->
        logger.quiet "Executing test ${desc.name} [${desc.className}] with result: ${result.resultType}"
    }
}

Notice the test output now:

Executing test testTwo() [com.howtodoinjava.junit5.examples.xmlReport.HelloTest] with result: SUCCESS
Executing test testOnProd() [com.howtodoinjava.junit5.examples.AssumptionsTest] with result: SUCCESS
Executing test testCaseA(TestInfo) [com.howtodoinjava.junit5.examples.ClassATest] with result: SUCCESS
Executing test testOnDev() [com.howtodoinjava.junit5.examples.AssumptionsTest] with result: SUCCESS
...
...

4. Filtering and Executing Tests with Gradle

Now let us see some examples to execute only specific tests filtered by test classes and methods. Note that the steps to run a test suite are the same as the test class.

4.1. Executing Only a Single Test

To run all tests only from a specific test class, use the –tests flag and pass the method name along with the class name as follows:

gradle test --tests com.howtodoinjava.junit5.examples.AppTest.testCalAdd

Program output:

> Task :test
Executing test testCalAdd() [com.howtodoinjava.junit5.examples.AppTest] with result: SUCCESS

4.2. Execute All Tests of a Test Class

To run all tests only from a specific test class, use the –tests flag as shown in the following command:

gradle test --tests com.howtodoinjava.junit5.examples.AppTest

Program output:

> Task :test
Executing test testCalcTwo() [com.howtodoinjava.junit5.examples.AppTest] with result: SKIPPED
Executing test testCalAdd() [com.howtodoinjava.junit5.examples.AppTest] with result: SUCCESS
Executing test testCalSubtract() [com.howtodoinjava.junit5.examples.AppTest] with result: SUCCESS
Executing test testCalMultiply() [com.howtodoinjava.junit5.examples.AppTest] with result: SUCCESS

4.3. Execute All Tests of Multiple Test Classes

Similar to the previous example, we can repeat all the test classes with ‘–tests‘ flag and gradle will run all tests.

gradle test --tests com.howtodoinjava.junit5.examples.AppTest --tests /
 com.howtodoinjava.junit5.examples.ordering.CustomOrderTests                                

Program output:

> Task :test
Executing test testCalSubtract() [com.howtodoinjava.junit5.examples.AppTest] with result: SUCCESS
Executing test testCalMultiply() [com.howtodoinjava.junit5.examples.AppTest] with result: SUCCESS
...
Executing test testA() [com.howtodoinjava.junit5.examples.ordering.CustomOrderTests] with result: SUCCESS
...
...

4.4. Execute Tests from Multiple Classes and Packages using Regex

To run all tests that are present in several classes in a package, or scattered in several packages, we can use regular expressions to scan for those classes.

For example, the following command will execute all tests from package ‘com.howtodoinjava.junit5.examples‘.

gradle test --tests com.howtodoinjava.junit5.examples.* 

Similarly, we can run all tests in the same package that starts with the name “Test” as follows:

gradle test --tests com.howtodoinjava.junit5.examples.Test* 

We can execute all tests that match a method name using regex, no matter where the test is present inside the hierarchy.

gradle test --tests com.howtodoinjava.junit5.examples.*.testCalSubtract

5. Re-run the Failed Tests

To rerun failed tests with Gradle from the command line, we can use the --rerun-tasks option. This option allows to rerun tasks that failed during the previous Gradle build, including test tasks.

We can specify how many times to rerun by adding a numeric value after the option. For example, to rerun failed tests only once:

gradle test --rerun-tasks 1

If you want to keep rerunning until all tests pass, you can use a high number or -1 to indicate an indefinite number of reruns.

gradle test --rerun-tasks -1

6. Conclusion

In this article, we’ve explored the essential steps for running JUnit tests with Gradle, demonstrating the simplicity and flexibility that Gradle offers for test automation.

By configuring test tasks, and utilizing the --tests option, we can effortlessly run tests at various granularities, from entire test suites to specific test classes or methods.

Happy Learning !!

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode