How to Run JUnit 5 Tests - ConsoleLauncher, Gradle
Using ConsoleLauncher
JUnit 5 provides a command-line tool called ConsoleLauncher. Because it is distributed as a JAR file, download it from the Maven Central Repository. In this example, version 1.9.1 was used.
Running Tests
Test classes are usually located under build/classes/java/test or target/test-classes.
Run all tests on the test classpath:
$ java -jar junit-platform-console-standalone-1.9.1.jar \
--classpath build/classes/java/test \
--scan-classpath
--classpath can be shortened to -cp. The standalone JAR already includes JUnit 5 classes, and -cp can additionally include dependencies used by the tests.
Run a specific class:
$ java -jar junit-platform-console-standalone-1.9.1.jar \
-cp build/classes/java/test \
--select-class com.devkuma.junit5.JUnit5Test
--select-class can be shortened to -c. You can also select a package with --select-package or filter class names with --include-classname and a regular expression.
Test Output
Test results are printed to the console. Successful tests, failed tests, failure details, container counts, and test counts are all included in the output.
Command Help
ConsoleLauncher provides many more options. Check them with:
$ java -jar junit-platform-console-standalone-1.9.1.jar --help
Running Tests with Gradle
Most projects run tests through their build tool. Gradle supports JUnit 5 by default from Gradle 4.6.
Gradle Configuration
Gradle also supports other test frameworks such as JUnit 4 and TestNG. To use JUnit 5, explicitly declare useJUnitPlatform().
build.gradle
test {
useJUnitPlatform()
}
Running Test Code
If all tests pass, Gradle reports BUILD SUCCESSFUL. If failures exist, the test task fails and Gradle prints the failing test names and the report path, such as build/reports/tests/test/index.html.
Static nested classes can be executed as tests, while ordinary inner classes are not run by default.