In this article, we will show you how to setup and run JUnit 5 examples with Gradle so that you can get started with JUnit 5, the new generation of JUnit. If you’re interested in setting setting up JUnit 5 with Maven, please see JUnit 5 Maven Example
1. Prerequisites
- JUnit 5 requires Java 8 or above.
- Gradle 2.5 or higher installed in your environment. You can refer to the following guides to install Gradle on Linux or Windows.
Install Gradle on Ubuntu 16.04 LTS (Xenial Xerus)
2. Configure JUnit 5 with Gradle
Here is where you can configure in detail for the plugin when running the JUnit 5 tests, for example:
1 2 3 4 5 6 |
test { useJUnitPlatform() testLogging { events "passed", "skipped", "failed" } } |
2.3. Configuring Test Engines
Test Engine is a new concept in JUnit 5, which “facilitates discovery and execution of tests for a particular programming model”. Generally speaking, if we want to provide a different way to discover and execute our tests, we can implement a new Test Engine. JUnit Jupiter is the new programming model and extension model in JUnit 5. Therefore, we need to configure it in the build file.
1 2 3 4 5 |
dependencies { testCompile('org.junit.jupiter:junit-jupiter-api:5.3.0') testCompile('org.junit.jupiter:junit-jupiter-params:5.3.0') testRuntime('org.junit.jupiter:junit-jupiter-engine:5.3.0') } |
JUnit Jupiter doesn’t support running JUnit 3, JUnit 4. To support those old JUnit tests, we have to configure and use the JUnit Vintage.
1 2 3 4 |
dependencies { testCompile("junit:junit:4.12") testRuntime("org.junit.vintage:junit-vintage-engine:4.12.0") } |
2.4. The Completed build.gradle file
Because we need to integrate our project with IDE (Eclipse, IntelliJ, etc), we need more configuration beside above ones. We can come up with the completed build.gradle file as the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
plugins { id 'java' id 'eclipse' // optional (to generate Eclipse project files) id 'idea' // optional (to generate IntelliJ IDEA project files) } repositories { mavenCentral() } dependencies { testCompile('org.junit.jupiter:junit-jupiter-api:5.3.0') testCompile('org.junit.jupiter:junit-jupiter-params:5.3.0') testRuntime('org.junit.jupiter:junit-jupiter-engine:5.3.0') } test { useJUnitPlatform() testLogging { events "passed", "skipped", "failed" } } wrapper { gradleVersion = '4.8' } |
3. Run JUnit 5 with Gradle
3.1. With Gradle
We can execute the following command to run the JUnit 5 tests with Gradle.
1 |
gradle clean test |
or
1 |
gradle clean junitPlatformTest |
3.2. With Gradle Wrapper
1 |
gradlew clean test |
or
1 |
gradlew clean junitPlatformTest |
Here is example output:
4. Summary
We have seen how to setup and run JUnit 5 examples with Gradle. However, the JUnit 5 is still not finally released yet; we will keep this article updated if any. The sample source code for this tutorial can be found on Github project or download by this link: junit5-sample.zip
If you want to get started with JUnit 5 tests, annotations, assertions, etc, you can refer to the following links:
JUnit 5 Basic Introduction – Getting Started With The Next Generation of JUnit
JUnit 5 Disable or Ignore A Test
JUnit 5 Dynamic Tests – Generate Tests at Run-time
JUnit 5 Test Suite – Aggregating Tests In Suites
JUnit 5 Assumptions With Assume
JUnit 5 Parameter Resolution Example
Display Names and Technical Names in JUnit 5
I am getting an error while runnign the test
Task :junitPlatformTest
Exception in thread “main” java.lang.NoClassDefFoundError: org/apache/logging/log4j/util/ReflectionUtil
Could you provide more detail about the command that you use?
For example, have you tried?
gradlew test
or
gradlew junitPlatformTest