4.2. Build Support

4.2.1. Gradle

The JUnit Platform Gradle Plugin has been discontinued

The junit-platform-gradle-plugin developed by the JUnit team was deprecated in JUnit Platform 1.2 and discontinued in 1.3. Please switch to Gradle’s standard test task.

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:

  1. test {
  2. useJUnitPlatform()
  3. }

Filtering by tags or engines is also supported:

  1. test {
  2. useJUnitPlatform {
  3. includeTags 'fast', 'smoke & feature-a'
  4. // excludeTags 'slow', 'ci'
  5. includeEngines 'junit-jupiter'
  6. // excludeEngines 'junit-vintage'
  7. }
  8. }

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.

  1. test {
  2. // ...
  3. systemProperty 'junit.jupiter.conditions.deactivate', '*'
  4. systemProperties = [
  5. 'junit.jupiter.extensions.autodetection.enabled': 'true',
  6. 'junit.jupiter.testinstance.lifecycle.default': 'per_class'
  7. ]
  8. // ...
  9. }
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.

  1. dependencies {
  2. testImplementation("org.junit.jupiter:junit-jupiter-api:5.7.0")
  3. testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.7.0")
  4. }

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.

  1. dependencies {
  2. testImplementation("junit:junit:4.13")
  3. testRuntimeOnly("org.junit.vintage:junit-vintage-engine:5.7.0")
  4. }
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).

  1. test {
  2. systemProperty 'java.util.logging.manager', 'org.apache.logging.log4j.jul.LogManager'
  3. }

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 junit-platform-surefire-provider, which was originally developed by the JUnit team, was deprecated in JUnit Platform 1.3 and discontinued in 1.4. Please use Maven Surefire’s native support instead.

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.

  1. <build>
  2. <plugins>
  3. <plugin>
  4. <artifactId>maven-surefire-plugin</artifactId>
  5. <version>2.22.2</version>
  6. </plugin>
  7. <plugin>
  8. <artifactId>maven-failsafe-plugin</artifactId>
  9. <version>2.22.2</version>
  10. </plugin>
  11. </plugins>
  12. </build>
  13. <!-- ... -->
  14. <dependencies>
  15. <!-- ... -->
  16. <dependency>
  17. <groupId>org.junit.jupiter</groupId>
  18. <artifactId>junit-jupiter-api</artifactId>
  19. <version>5.7.0</version>
  20. <scope>test</scope>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.junit.jupiter</groupId>
  24. <artifactId>junit-jupiter-engine</artifactId>
  25. <version>5.7.0</version>
  26. <scope>test</scope>
  27. </dependency>
  28. <!-- ... -->
  29. </dependencies>
  30. <!-- ... -->

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.

  1. <!-- ... -->
  2. <build>
  3. <plugins>
  4. <plugin>
  5. <artifactId>maven-surefire-plugin</artifactId>
  6. <version>2.22.2</version>
  7. </plugin>
  8. <plugin>
  9. <artifactId>maven-failsafe-plugin</artifactId>
  10. <version>2.22.2</version>
  11. </plugin>
  12. </plugins>
  13. </build>
  14. <!-- ... -->
  15. <dependencies>
  16. <!-- ... -->
  17. <dependency>
  18. <groupId>junit</groupId>
  19. <artifactId>junit</artifactId>
  20. <version>4.13</version>
  21. <scope>test</scope>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.junit.vintage</groupId>
  25. <artifactId>junit-vintage-engine</artifactId>
  26. <version>5.7.0</version>
  27. <scope>test</scope>
  28. </dependency>
  29. <!-- ... -->
  30. </dependencies>
  31. <!-- ... -->
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

  1. <!-- ... -->
  2. <build>
  3. <plugins>
  4. <plugin>
  5. <artifactId>maven-surefire-plugin</artifactId>
  6. <version>2.22.2</version>
  7. <configuration>
  8. <excludes>
  9. <exclude/>
  10. </excludes>
  11. </configuration>
  12. </plugin>
  13. </plugins>
  14. </build>
  15. <!-- ... -->

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.

  1. <!-- ... -->
  2. <build>
  3. <plugins>
  4. <plugin>
  5. <artifactId>maven-surefire-plugin</artifactId>
  6. <version>2.22.2</version>
  7. <configuration>
  8. <groups>acceptance | !feature-a</groups>
  9. <excludedGroups>integration, regression</excludedGroups>
  10. </configuration>
  11. </plugin>
  12. </plugins>
  13. </build>
  14. <!-- ... -->
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.

  1. <!-- ... -->
  2. <build>
  3. <plugins>
  4. <plugin>
  5. <artifactId>maven-surefire-plugin</artifactId>
  6. <version>2.22.2</version>
  7. <configuration>
  8. <properties>
  9. <configurationParameters>
  10. junit.jupiter.conditions.deactivate = *
  11. junit.jupiter.extensions.autodetection.enabled = true
  12. junit.jupiter.testinstance.lifecycle.default = per_class
  13. </configurationParameters>
  14. </properties>
  15. </configuration>
  16. </plugin>
  17. </plugins>
  18. </build>
  19. <!-- ... -->

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).

  1. <path id="test.classpath">
  2. <!-- The location where you have your compiled classes -->
  3. <pathelement location="${build.classes.dir}" />
  4. </path>
  5. <!-- ... -->
  6. <junitlauncher>
  7. <classpath refid="test.classpath" />
  8. <test name="org.myapp.test.MyFirstJUnit5Test" />
  9. </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.

  1. <path id="test.classpath">
  2. <!-- The location where you have your compiled classes -->
  3. <pathelement location="${build.classes.dir}" />
  4. </path>
  5. <!-- ... -->
  6. <junitlauncher>
  7. <classpath refid="test.classpath" />
  8. <testclasses outputdir="${output.dir}">
  9. <fileset dir="${build.classes.dir}">
  10. <include name="org/example/**/demo/**/" />
  11. </fileset>
  12. <fileset dir="${some.other.dir}">
  13. <include name="org/myapp/**/" />
  14. </fileset>
  15. </testclasses>
  16. </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.