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 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:

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.

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.

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:

3. Run JUnit 5 with Gradle

3.1. With Gradle

We can execute the following command to run the JUnit 5 tests with Gradle.

or

3.2. With Gradle Wrapper

or

Here is example output:

JUnit 5 with Gradle - Sample output

JUnit 5 examples with Gradle – Sample 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 Tutorial

JUnit 5 Annotations Example

JUnit 5 Basic Introduction – Getting Started With The Next Generation of JUnit

JUnit 5 Maven Example

JUnit 5 vs JUnit 4

JUnit 5 Assertions Example

JUnit 5 Disable or Ignore A Test

JUnit 5 Exception Testing

JUnit 5 Dynamic Tests – Generate Tests at Run-time

JUnit 5 Nested Tests Examples

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

 

 

0 0 vote
Article Rating