JUnit 5 – Assertions and Assumptions (Part 2)

The second part of our article on assertions and assumptions in JUnit 5. This time we are looking at assumptions.

The second part of our article on assertions and assumptions in JUnit 5. This time we are looking at assumptions.

2. Assumptions in JUnit 5

JUnit Jupiter comes with a subset of the assumption methods that JUnit 4 provides. JUnit Jupiter also adds a few assumption methods that lend themselves well to being used with Java 8 lambdas. All JUnit Jupiter assumptions are static methods in the org.junit.jupiter.api.Assumptions class. The message parameter is on the last position.

The assertions are executed only in case that the assumption is fulfilled.

The method looks like this:

Assumptions in JUnit 5.JPG


A comparison between assertions in JUnit 4 and JUnit 5 looks like this:

comparison between assertions in JUnit 4 and JUnit 5.png


This is how the usage of the assume methods may look like:

ussage of assume methods.JPG


Conclusions

JUnit 5 comes with a new and flexible API for assertions and assumptions. I provides a lot of static methods and adapted to the capabilities of the functional programming introduces with Java 8. The reader should have a good image of the new features and, from here, new steps can be made in the search of what JUnit 5 can provide. From what this article does not analyze, dynamic and parameterized tests may be the next logical areas to investigate.

Interested in JUnit? Check out our trainings.


Catalin Tudose
Java and Web Technologies Expert

Share the knowledge

Luxoft Warsaw - Warsaw Spire, plac Europejski 1, 00-844 Warszawa
Dimitrie Pompeiu nr 5-7 , building C, Et. 5, sect 2, Bucharest, 014459

Contact phone:

021 371 4858
Luxoft Poland Wroclaw - Silver Tower pl. Konstytucji 3-go Maja 3 50-048 Wroclaw
Aleja Generała Tadeusza Bora-Komorowskiego 25, Quattro Business Park Five, 31-476 Kraków, Poland

Contact phone:

+48 122110650
Success
Thank you.
Your request has been received.