4.2. Build Support
4.2.1. Gradle
The JUnit Platform Gradle Plugin has been discontinued The |
Starting with version 4.6, Gradle provides native support for executing tests on the JUnit Platform. To enable it, you just need to specify useJUnitPlatform()
within a test
task declaration in build.gradle
:
test {
useJUnitPlatform()
}
Filtering by tags or engines is also supported:
test {
useJUnitPlatform {
includeTags 'fast', 'smoke & feature-a'
// excludeTags 'slow', 'ci'
includeEngines 'junit-jupiter'
// excludeEngines 'junit-vintage'
}
}
Please refer to the official Gradle documentation for a comprehensive list of options.
Configuration Parameters
The standard Gradle test
task currently does not provide a dedicated DSL to set JUnit Platform configuration parameters to influence test discovery and execution. However, you can provide configuration parameters within the build script via system properties (as shown below) or via the junit-platform.properties
file.
test {
// ...
systemProperty 'junit.jupiter.conditions.deactivate', '*'
systemProperties = [
'junit.jupiter.extensions.autodetection.enabled': 'true',
'junit.jupiter.testinstance.lifecycle.default': 'per_class'
]
// ...
}
Configuring Test Engines
In order to run any tests at all, a TestEngine
implementation must be on the classpath.
To configure support for JUnit Jupiter based tests, configure a testImplementation
dependency on the JUnit Jupiter API and a testRuntimeOnly
dependency on the JUnit Jupiter TestEngine
implementation similar to the following.
dependencies {
testImplementation("org.junit.jupiter:junit-jupiter-api:5.7.0")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.7.0")
}
The JUnit Platform can run JUnit 4 based tests as long as you configure a testImplementation
dependency on JUnit 4 and a testRuntimeOnly
dependency on the JUnit Vintage TestEngine
implementation similar to the following.
dependencies {
testImplementation("junit:junit:4.13")
testRuntimeOnly("org.junit.vintage:junit-vintage-engine:5.7.0")
}
Configuring Logging (optional)
JUnit uses the Java Logging APIs in the java.util.logging
package (a.k.a. JUL) to emit warnings and debug information. Please refer to the official documentation of [LogManager](https://docs.oracle.com/javase/8/docs/api/java/util/logging/LogManager.html)
for configuration options.
Alternatively, it’s possible to redirect log messages to other logging frameworks such as Log4j or Logback. To use a logging framework that provides a custom implementation of [LogManager](https://docs.oracle.com/javase/8/docs/api/java/util/logging/LogManager.html)
, set the java.util.logging.manager
system property to the fully qualified class name of the [LogManager](https://docs.oracle.com/javase/8/docs/api/java/util/logging/LogManager.html)
implementation to use. The example below demonstrates how to configure Log4j 2.x (see Log4j JDK Logging Adapter for details).
test {
systemProperty 'java.util.logging.manager', 'org.apache.logging.log4j.jul.LogManager'
}
Other logging frameworks provide different means to redirect messages logged using java.util.logging
. For example, for Logback you can use the JUL to SLF4J Bridge by adding an additional dependency to the runtime classpath.
4.2.2. Maven
The JUnit Platform Maven Surefire Provider has been discontinued The |
Starting with version 2.22.0, Maven Surefire and Maven Failsafe provide native support for executing tests on the JUnit Platform. The pom.xml
file in the [junit5-jupiter-starter-maven](https://github.com/junit-team/junit5-samples/tree/r5.7.0/junit5-jupiter-starter-maven)
project demonstrates how to use the Maven Surefire plugin and can serve as a starting point for configuring your Maven build.
Configuring Test Engines
In order to have Maven Surefire or Maven Failsafe run any tests at all, at least one TestEngine
implementation must be added to the test classpath.
To configure support for JUnit Jupiter based tests, configure test
scoped dependencies on the JUnit Jupiter API and the JUnit Jupiter TestEngine
implementation similar to the following.
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
</build>
<!-- ... -->
<dependencies>
<!-- ... -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
<!-- ... -->
</dependencies>
<!-- ... -->
Maven Surefire and Maven Failsafe can run JUnit 4 based tests alongside Jupiter tests as long as you configure test
scoped dependencies on JUnit 4 and the JUnit Vintage TestEngine
implementation similar to the following.
<!-- ... -->
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
</build>
<!-- ... -->
<dependencies>
<!-- ... -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
<!-- ... -->
</dependencies>
<!-- ... -->
Filtering by Test Class Names
The Maven Surefire Plugin will scan for test classes whose fully qualified names match the following patterns.
**/Test*.java
**/*Test.java
**/*Tests.java
**/*TestCase.java
Moreover, it will exclude all nested classes (including static member classes) by default.
Note, however, that you can override this default behavior by configuring explicit include
and exclude
rules in your pom.xml
file. For example, to keep Maven Surefire from excluding static member classes, you can override its exclude rules as follows.
Overriding exclude rules of Maven Surefire
<!-- ... -->
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<excludes>
<exclude/>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<!-- ... -->
Please see the Inclusions and Exclusions of Tests documentation for Maven Surefire for details.
Filtering by Tags
You can filter tests by tags or tag expressions using the following configuration properties.
to include tags or tag expressions, use
groups
.to exclude tags or tag expressions, use
excludedGroups
.
<!-- ... -->
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<groups>acceptance | !feature-a</groups>
<excludedGroups>integration, regression</excludedGroups>
</configuration>
</plugin>
</plugins>
</build>
<!-- ... -->
Configuration Parameters
You can set JUnit Platform configuration parameters to influence test discovery and execution by declaring the configurationParameters
property and providing key-value pairs using the Java Properties
file syntax (as shown below) or via the junit-platform.properties
file.
<!-- ... -->
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<properties>
<configurationParameters>
junit.jupiter.conditions.deactivate = *
junit.jupiter.extensions.autodetection.enabled = true
junit.jupiter.testinstance.lifecycle.default = per_class
</configurationParameters>
</properties>
</configuration>
</plugin>
</plugins>
</build>
<!-- ... -->
4.2.3. Ant
Starting with version 1.10.3
of Ant, a new junitlauncher
task has been introduced to provide native support for launching tests on the JUnit Platform. The junitlauncher
task is solely responsible for launching the JUnit Platform and passing it the selected collection of tests. The JUnit Platform then delegates to registered test engines to discover and execute the tests.
The junitlauncher
task attempts to align as close as possible with native Ant constructs such as resource collections for allowing users to select the tests that they want executed by test engines. This gives the task a consistent and natural feel when compared to many other core Ant tasks.
Starting with version 1.10.6
of Ant, the junitlauncher
task supports forking the tests in a separate JVM.
The build.xml
file in the [junit5-jupiter-starter-ant](https://github.com/junit-team/junit5-samples/tree/r5.7.0/junit5-jupiter-starter-ant)
project demonstrates how to use the task and can serve as a starting point.
Basic Usage
The following example demonstrates how to configure the junitlauncher
task to select a single test class (i.e., org.myapp.test.MyFirstJUnit5Test
).
<path id="test.classpath">
<!-- The location where you have your compiled classes -->
<pathelement location="${build.classes.dir}" />
</path>
<!-- ... -->
<junitlauncher>
<classpath refid="test.classpath" />
<test name="org.myapp.test.MyFirstJUnit5Test" />
</junitlauncher>
The test
element allows you to specify a single test class that you want to be selected and executed. The classpath
element allows you to specify the classpath to be used to launch the JUnit Platform. This classpath will also be used to locate test classes that are part of the execution.
The following example demonstrates how to configure the junitlauncher
task to select test classes from multiple locations.
<path id="test.classpath">
<!-- The location where you have your compiled classes -->
<pathelement location="${build.classes.dir}" />
</path>
<!-- ... -->
<junitlauncher>
<classpath refid="test.classpath" />
<testclasses outputdir="${output.dir}">
<fileset dir="${build.classes.dir}">
<include name="org/example/**/demo/**/" />
</fileset>
<fileset dir="${some.other.dir}">
<include name="org/myapp/**/" />
</fileset>
</testclasses>
</junitlauncher>
In the above example, the testclasses
element allows you to select multiple test classes that reside in different locations.
For further details on usage and configuration options please refer to the official Ant documentation for the junitlauncher
task.