Search⌘ K
AI Features

Assertions: assertNull() and assertNotNull()

Explore how JUnit 5's assertNull and assertNotNull methods help verify whether objects are null or not. Understand their overloads with optional failure messages, including message suppliers, to write precise unit tests.

The assertNull() method

The assertNull() method asserts that the given Object is null.

  • If the actual value is null, the test case passes.
  • If the actual value is not null, the test case fails.

It has three different overloaded methods:

JUnit5 v5.8
public static void assertNull(Object actual)
public static void assertNull(Object actual, String message)
public static void assertNull(Object actual, Supplier<String> messageSupplier)
  • The assertNull(Object actual) method is the simplest form that only accepts a single Object to check.
  • For the assertNull(Object actual, String message) method, when the Object isn’t
...