JUnit Jupiter, a module of JUnit 5 which is the next generation of JUnit 4, comes with many of assertion methods that JUnit 4 has and adds some overloads which can be used with Java 8 Lambda Expression. In this post, we will get through some examples that use both JUnit 5 assertions borrowed from JUnit 4 and JUnit 5 assertions with Java 8 Lambda Expression.

1. Preparations.

All the source code represented in this tutorial is available on the Github project.

To run the source code, we will need IDE such as Eclipse, IntelliJ or build tools like Maven, Gradle. You can refer to my recent post for guides on how to get started with JUnit 5: JUnit 5 Basic Introduction

2. JUnit 5 Assertions Example

Assume that we will write JUnit tests for below method which is used to convert a given string into Double.


2.1. AssertAll, AssertNotNull, and AssertEquals

We will test the method convertToDouble with a test case that we will pass a parameter with a right Double value string. AssertAll is a new assertion in JUnit 5 which allows us to execute a group of assertions in a bigger one.

2.2. AssertNull

In this example, we will test the method convertToDouble by passing a parameter as a NULL value.

2.3. JUnit 5 AssertThrows

In this example, we will test the method convertToDouble by passing a not numeric string as a parameter of the method. Note that in this case, the method will call: Double.value(parameter) to convert the string to Double and we will get the NumberFormatException exception.

Note that AssertThrows is the new assertion in JUnit 5.

2.3. AssertTrue and AssertFalse

Assume that we will write JUnit tests for the following method:

And we will write 3 test cases for this method by passing: NULL, empty and not empty string as parameters of the method.

2.4. AssertSame, AssertNotSame, and Fail

Assume that we will write JUnit tests for the following method which will test if a given string is null or not. If it is null then return the given default string.

Here is our test method which using JUnit 5 assertions: assertSame, assertNotSame, and fail

3. Test results

Just to show the tests run on Eclipse:

JUnit 5 Assertions Examples

JUnit 5 Assertions Example Results

4. Conclusions

We have just tried to write some very basic Unit tests using JUnit 5, especially use JUnit 5 Assertions to support us in asserting the test results. We may see that JUnit 5 provides us with very basic assertions like JUnit 4. Beside, JUnit 5 provides some more assertions such as assertThrow, assertAll, and some overloads to support Java 8 Lambda Expression as well. Hope you can enjoy learning some basic steps to write Unit tests using JUnit 5 assertions. In the next posts, I’d like to share more about JUnit 5 features like assumptions, extension, etc. If you’re interested in JUnit 5, you can refer to my JUnit 5 tutorial page for more tutorials, examples.

JUnit 5 Tutorial

JUnit 5 with Gradle Example

JUnit 5 Maven Example

JUnit 5 Dynamic Tests – Generate Tests at Run-time

JUnit 5 vs JUnit 4

JUnit 5 Annotations Example

JUnit 5 Disable or Ignore A Test

JUnit 5 Exception Testing

JUnit 5 Dynamic Tests – Generate Tests at Run-time

JUnit 5 Nested Tests Examples

JUnit 5 Test Suite – Aggregating Tests In Suites

Display Names and Technical Names in JUnit 5



5 1 vote
Article Rating