Assertion facilities provided by JUnit Jupiter or Junit 5 are sufficient for most testing scenarios, there are times when we need more power and additional functionality such as matchers are desired. In that case, the JUnit team recommends the use of third-party assertion libraries such as AssertJ, Truth,Hamcrest, etc. Developers are free to use the assertion library of their own choices.
For example, the combination of matchers and a fluent API can be used to make assertions more readable and descriptive. However, JUnit Jupiter’s org.junit.jupiter.api.Assertions class does not provide an assertThat() method like the one available in JUnit 4’s org.junit.Assert class which accepts a Hamcrest Matcher. Instead, developers are recommended to use the built-in support for matchers provided by third-party assertion libraries.
This example demonstrates how to use the assertThat() support from Hamcrest in a JUnit Jupiter test. As long as the Hamcrest library has been added to the classpath, We can statically import methods such as assertThat(),equalTo(), and is() and then use them in tests like in the test_add_two_positive_numbers() method below.
pom.xml
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 |
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.kkjavatutorials</groupId> <artifactId>JUnit5ThirdPartyAssertionLibrariesExample</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <maven.compiler.target>8</maven.compiler.target> <maven.compiler.source>8</maven.compiler.source> <junit.jupiter.version>5.6.2</junit.jupiter.version> <hamcrest.version>1.3</hamcrest.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-engine</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-all</artifactId> <version>${hamcrest.version}</version> <scope>test</scope> </dependency> </dependencies> </project> |
Resource class:
1 2 3 4 5 6 7 8 |
package com.kkjavatutorials; public class MyUtils { public int add(int n1, int n2) { return n1+n2; } } |
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 |
package com.kkjavatutorials; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; public class MyUtilsTest { private static MyUtils myUtils; @BeforeAll static void setUp() { myUtils = new MyUtils(); System.out.println("Test data set up is done.."); } @BeforeEach void beforeEach() { System.out.println("@BeforeEach is executed.."); } @Test void test_add_two_positive_numbers() { int actualResult = myUtils.add(20, 10); assertThat(actualResult, is(equalTo(30))); } @AfterEach void afterEach() { System.out.println("@AfterEach is executed.."); } @AfterAll static void tearDown() { myUtils = null; System.out.println("Test data teardown is done.."); } } |
The output of the above project:
You May Also Like:
Junit 5 Architecture
JUnit 5 Annotations
JUnit 5 Maven Dependency
JUnit 5 with Gradle Dependency
JUnit 5 Test Lifecycle
JUnit 5 @BeforeAll annotation example
Unit 5 @AfterAll annotation example
JUnit 5 @BeforeEach and @AfterEach annotation Example
That’s all about Third-party Assertion Libraries support in JUnit 5
If you have any feedback or suggestion please feel free to drop in below comment box.