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
1 2 3 4 5 6 |
<dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.3.0</version> <scope>test</scope> </dependency> |
2.2. JUnit 5 Maven Surefire Provider
1 2 3 4 5 6 7 8 9 |
<build> <plugins> <!-- JUnit 5 requires Surefire version 2.22.0 or higher --> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.0</version> </plugin> </plugins> </build> |
2.3. The Full pom.xml File
We can come up with the full pom.xml file as 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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>junit5-maven-example</artifactId> <version>1.0-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>${maven.compiler.source}</maven.compiler.target> <junit.jupiter.version>5.3.0</junit.jupiter.version> </properties> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-params</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <!-- JUnit 5 requires Surefire version 2.22.0 or higher --> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.0</version> </plugin> </plugins> </build> </project> |
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.
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 27 28 |
package com.howtoprogram.junit5; public class BasicSalaryCalculator { private double basicSalary; public double getBasicSalary() { return basicSalary; } public void setBasicSalary(double basicSalary) { if (basicSalary < 0) { throw new IllegalArgumentException("Negative salary is invalid."); } this.basicSalary = basicSalary; } public double getGrossSalary() { return this.basicSalary + getSocialInsurance() + getAdditionalBonus(); } public double getSocialInsurance() { return this.basicSalary * 25 / 100; } public double getAdditionalBonus() { return this.basicSalary / 10; } } |
Here is an example of a test class.
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
package com.howtoprogram.junit5; import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; public class BasicSalaryCalculatorTest { private BasicSalaryCalculator basicSalaryCalculator; @BeforeEach void init() { basicSalaryCalculator = new BasicSalaryCalculator(); } @Test void testBasicSalaryWithValidSalary() { double basicSalary = 4000; basicSalaryCalculator.setBasicSalary(basicSalary); double expectedSocialInsurance = basicSalary * 0.25; assertEquals(expectedSocialInsurance, basicSalaryCalculator.getSocialInsurance()); double expectedAddionalBonus = basicSalary * 0.1; assertEquals(expectedAddionalBonus, basicSalaryCalculator.getAdditionalBonus()); double expectedGross = basicSalary + expectedSocialInsurance + expectedAddionalBonus; assertEquals(expectedGross, basicSalaryCalculator.getGrossSalary()); } @DisplayName("Test BasicSalaryCalculator with invalid salary") @Test void testBasicSalaryWithInValidSalary() { double basicSalary = -100; assertThrows(IllegalArgumentException.class, () -> { basicSalaryCalculator.setBasicSalary(basicSalary); }); } @AfterEach void tearDown() { basicSalaryCalculator = null; } } |
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:
1 |
mvn test |
Here is the generated output of above example:
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 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
Your codes giving errors.
org.junit.platform.commons.JUnitException: @BeforeAll method must be static unless the test class is annotated with @TestInstance(Lifecycle.PER_CLASS).