In this article, I’d like to show you how to setup and run a JUnit 5 example with Maven so that you can get started with new JUnit 5 features.

1. Prerequisites

JUnit 5 requires Java 8 or above. Therefore, you should have it installed first. To use JUnit 5 with IDE such as Eclipse, IntelliJ, you can take a look at another tutorial: Introduction to JUnit 5

2. JUnit 5 Maven Dependency

There are 2 dependencies we need to specify in the pom.xml file in order to run JUnit 5 with Maven:

  • JUnit 5 library dependency for annotations, assertions, etc.
  • JUnit 5 maven surefire provider which is used during the test phase of the build lifecycle to execute the unit tests of an application.

2.1. JUnit 5 Library Dependency

The minimum JUnit 5 Maven dependency is: junit-jupiter-engine

2.2. JUnit 5 Maven Surefire Provider

2.3. The Full pom.xml File

We can come up with the full pom.xml file as following:

However, with this pom.xml file, we just can run the tests using Maven while we need to write tests first which will be done easily with IDE such as Eclipse, IntelliJ. So, if you need a pom.xml file, you can obtain it on my github project.

3. JUnit 5 Example Test

Let’s say we have a class BasicSalaryCalculator. java as the following and we want to write some tests for this class.

Here is an example of a test class.

4. Run JUnit 5 Tests with Maven

To run JUnit 5 with Maven, we simply open the terminal, go the project directory and issue the command:

Here is the generated output of above example:

JUnit 5 Maven Example

Run JUnit 5 Example with Maven

5. Summary

The tutorial has illustrated how to setup and run JUnit 5 with Maven by a simple example. We just need one dependency and one JUnit 5 Maven Surefire plugin, and running JUnit 5 with Maven will be the same with previous versions of JUnit. The sample source code for this tutorial can be found on Github project or download by this link: junit5-sample.zip

Below are other articles related to JUnit 5, if you’re interested in, you can refer to the following links:

JUnit 5 Tutorial

JUnit 5 with Gradle Example

JUnit 5 Annotations Example

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

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

 

 

 

 

0 0 vote
Article Rating