JUnit 5 – new approach, new functionalities. Part 3

JUnit 5 – new approach, new functionalities. Part 3

Assertions in Junit 5

 

Junit has more assertion type methods than Junit 4. Several have been added and they work very well together with the lambda expressions in Java 8. All these types of methods are static and come from the org.junit.jupiter.api.Assertions class.

 

The comparison between assertions in Junit 4 and Junit 5 look like this:

 

 JUnit 4 JUnit 5 
 Assert Class
The optional message is the first parameter
Assertions Class
The optional message is the last parameter
The assertThat method has been removed
New methods: assertAll and assertThrows

 

The message in assertions is the last parameter of the methods:

 

assertions_methods_JUnit5.jpg

 

The supplier allows for lazy initialization in case of complex messages. A chunk of code which uses possible assertions looks like this:

 

possible assertions code.jpg
junit5.jpg

Interested in JUnit?

Check out our trainings
Tudose, Florin-Catalin , Java Champion; Java Chapter Lead; Author at Pluralsight and Manning

Tudose, Florin-Catalin Tudose author linkedin

Java Champion; Java Chapter Lead; Author at Pluralsight and Manning

Tudose, Florin-Catalin , Java Champion; Java Chapter Lead; Author at Pluralsight and Manning

Tudose, Florin-Catalin Tudose author linkedin

Java Champion; Java Chapter Lead; Author at Pluralsight and Manning