JUnit 5 – Assertions and Assumptions (Part 2)

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.

 

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
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