JUnit 5 – new approach, new functionalities. Part 3
JUnit 5 – new approach, new functionalities. Part 3
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.
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:
The message in assertions is the last parameter of the methods:
![assertions_methods_JUnit5.jpg assertions_methods_JUnit5.jpg](http://luxoft-training.ro/upload/medialibrary/a01/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 possible assertions code.jpg](http://luxoft-training.ro/upload/medialibrary/11e/possible assertions code.jpg)
![junit5.jpg junit5.jpg](http://luxoft-training.ro/upload/medialibrary/7d0/junit5.jpg)
Catalin Tudose
Java and Web Technologies Expert
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 assertions_methods_JUnit5.jpg](http://luxoft-training.ro/upload/medialibrary/a01/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 possible assertions code.jpg](http://luxoft-training.ro/upload/medialibrary/11e/possible assertions code.jpg)
![junit5.jpg junit5.jpg](http://luxoft-training.ro/upload/medialibrary/7d0/junit5.jpg)
Interested in JUnit? Check out our trainings.
Catalin Tudose
Java and Web Technologies Expert