JUnit Assertions With Examples

JUnit Assertions allows us to write effective test methods. JUnit 5 is the latest version and JUnit Jupiter provides a lot of assertions to assert different types of statements.

JUnit Assertions

JUnit Jupiter org.junit.jupiter.api.Assertions class provides a collection of utility methods to use in our test code. All these methods are static, so we can import them and write fluent code. Almost all of these methods are overloaded to support primitives, objects, Collections, Stream, Arrays etc.

Let’s look at some of the important JUnit assert methods with an example.

fail()

This is used to fail a test, it’s useful when your test method is work in progress and you want to indicate that by fail-fast your test. There are many overloaded fail() methods, let’s look at some of them.

We can provide custom failure message and specify the cause of failure.

assertNull() and assertNotNull()

These methods are used to check if the specified object is null or not. We can also specify custom failure message.

assertSame() and assertNotSame()

These methods are used to assert that expected and actual elements are same or not. JUnit uses == operator for these methods to check if they are referring to same object or not.

assertTrue() and assertFalse()

Asserts that the supplied condition is true or false.

assertEquals() and assertNotEquals()

These two methods are used to assert that expected and actual objects are equal or not. They use equals() method of the object to check equality.

assertArrayEquals()

Asserts that expected and actual arrays are deeply equal. The arrays elements are matched index by index.

assertIterableEquals()

This method is used for iterables to check if they are equal or not. Note that the underlying implementation can be different. The elements are matched index by index for equality.

assertThrows()

Asserts that execution of the supplied executable throws an exception of the expectedType and returns the exception.

If no exception is thrown, or if an exception of a different type is thrown, this method will fail.

This method follows inheritance hierarchy, so the assert will pass if the expected type is Exception and actual is RuntimeException.

assertDoesNotThrow()

Asserts that execution of the supplied executable does not throw any kind of exception. This method takes org.junit.jupiter.api.function.Executable instance as argument. Executable is a functional interface that can be used to implement any generic block of code that potentially throws a Throwable.

We can use lambda expressions to create Executable if we are not planning to reuse it.

assertAll()

Asserts that all supplied executables do not throw exceptions. We can pass Array, Stream or a Collection of Executable objects.

assertTimeout()

Asserts that execution of the supplied executable completes before the given timeout is exceeded.

The executable will be executed in the same thread as that of the calling code. Consequently, execution of the executable will not be preemptively aborted if the timeout is exceeded.

assertTimeoutPreemptively()

Asserts that execution of the supplied executable completes before the given timeout is exceeded.

The executable will be executed in a different thread than that of the calling code. Furthermore, execution of the executable will be preemptively aborted if the timeout is exceeded.

JUnit Assert Examples Test

Below image shows the JUnit test results view in Eclipse when the test class was executed.

Summary

JUnit Jupiter provides a lot of assertions to help us write fluent test code. It’s always a good idea to import these assertion static methods and then write clean code.

By admin

Leave a Reply

%d bloggers like this: