Java TestNG Plugin

The Java TestNG plugin allows you to execute TestNG tests in a Java project. The features of the plugin are also the public methods of the plugin class, so you can refer to the Groovy Doc for more information.

LATEST VERSION: 2.2.2

Loading the plugin

Here is how you load this plugin:

javaTestNG = loadPlugin(id: "org.savantbuild.plugin:java-testng:2.2.2")

Settings

The Java TestNG plugin has one required setting, which specifies the Java version used to execute the tests with. The settings for the plugin are configured via the JavaTestNGSettings class, which is defined on the JavaTestNGPlugin class via a field named settings. Here is an example of configuring the plugin:

javaTestNG.settings.javaVersion = "17"

This property specifies the Java version to use, but you must also configure the location of the JDK on your computer. The location of the JDK is configured in the file ~/.config/savant/plugins/org.savantbuild.plugin.java.properties.

Here’s an example of the ~/.config/savant/plugins/org.savantbuild.plugin.java.properties file for a Mac:

1.8=/Users/me/dev/java/current8
17=/Users/me/dev/java/current17
21=/Users/me/dev/java/current21

This class has additional fields that allow you to configure the command-line arguments passed to the Java when it is run, the verbosity of the TestNG output, and the location of the test reports. Here are the additional settings:

JVM arguments

You can specify additional parameters to the JVM when TestNG executes your tests using the jvmArguments field on the settings class. Here is an example:

javaTestNG.settings.jvmArguments = "-Dsome.param=true"

TestNG arguments

You can specify additional TestNG arguments using the testngArguments field on the settings class. Here is an example:

javaTestNG.settings.testngArguments = "-parallel methods"

Listeners

You can specify custom TestNG listeners using the listeners field on the settings class. Here is an example:

javaTestNG.settings.listeners = ["com.mycompany.MyTestListener"]

Code coverage

The Java TestNG plugin has built-in support for JaCoCo code coverage. Enable it using the codeCoverage field on the settings class:

javaTestNG.settings.codeCoverage = true

When enabled, JaCoCo will instrument the test execution and produce a coverage report.

Verbosity

You can control the verbosity of the TestNG output using the verbosity field on the settings class. Here is an example:

javaTestNG.settings.verbosity = 10

The value is an integer where higher values mean more verbosity.

Report output directory

The directory that the test reports are output to can be changed using the reportDirectory field on the settings class. Here is an example:

javaTestNG.settings.reportDirectory = Paths.get("build/reports/tests")

Dependencies

You can configure the dependencies that are included when TestNG is run using the dependencies field on the settings class. Here is an example:

javaTestNG.settings.dependencies = [
    [group: "provided", transitive: true, fetchSource: false],
    [group: "compile", transitive: true, fetchSource: false],
    [group: "runtime", transitive: true, fetchSource: false],
    [group: "test-compile", transitive: true, fetchSource: false],
    [group: "test-runtime", transitive: true, fetchSource: false]
  ]

This configuration is the default. It ensures that the provided, compile, runtime, test-compile, test-runtime and all of their transitive dependencies in any groups are included. If you want to configure a different set of groups, here are the attributes you can use for each dependency definition (which are the Maps inside the list):

name description type required
group The dependency group to include String true
transitive Determines if transitive dependencies are included or not boolean true
fetchSource Determines if the source for the dependencies or downloaded or not boolean true
transitiveGroups The transitive dependency groups to fetch. This is only used if transitive is set to true List<String> false

Executing tests

This plugin provides a single method to run the tests. Here is an example of calling this method:

javaTestNG.test()

You can also control the TestNG groups that are run by passing in a set of groups to the test method like this:

groovyTestNG.test(groups: ["unit"])

You can exclude specific test groups:

javaTestNG.test(groups: ["unit"], exclude: ["slow"])

You can run one or more tests using the command-line test switch like this:

$ sb test --test=FooBarTest --test=BazTest

You can optionally provide a fully qualified test name if you have more than one test named FooBarTest. For example:

$ sb test --test=org.savantbuild.action.FooBarTest

You can optionally run only tests that failed from the last execution. This uses the test output from the previous run to determine which tests failed. If you perform a clean, this will not work.

$ sb test --onlyFailed

You can skip all the tests in a project using the command-line switch:

$ sb test --skipTests

You can run only tests that have changed since the last build:

$ sb test --onlyChanges

You can run only tests that have changed since a specific commit:

$ sb test --commitRange=HEAD~3  # Run tests affected by changes in the last 3 commits

You can keep the generated TestNG XML file after execution:

$ sb test --keepXML             # Keep the generated TestNG XML file after execution