Assertions in JUnit 5

By | October 2, 2020

JUnit Jupiter comes with many of the assertion methods that JUnit 4 already has and added a few more so that it may support Java 8 lambdas. Also in this library, assertions are present for all primitive types, Objects, and arrays (either of primitives or Objects). The assertions are a collection of utility methods that support asserting conditions in tests.

Assertions help us to validate the expected output with the actual output of test cases. 

All JUnit Jupiter assertions methods are static and present in the org.junit.jupiter.api.Assertions class.

Junit 5 Assertions

You will get below list of Assert methods(static methods and present in the org.junit.jupiter.api.Assertions class) are avialabe in Junit 5with Overloaded versions:

assertTrue
assertFalse
assertEquals
assertNotEquals
assertNull
assertNotNull
assertSame
assertNotSame
assertArrayEquals
assertAll
assertThrows
assertDoesNotThrow
assertIterableEquals
assertTimeout
assertTimeoutPreemptively
assertLinesMatch
fail

Let’s understand these assert methods using a below demo project.

pom.xml

Classes for that Junit test cases written:

Employee.java
MyUtils.java

Employee.java

 MyUtils.java

Test class JUnit5Assertions.java 

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
JUnit 5 Display Names

That’s all about Assertions in JUnit 5
If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

Your email address will not be published. Required fields are marked *