Search⌘ K
AI Features

assertNotNull() method

Explore how to apply the assertNotNull method in JUnit 5 to verify that objects are not null during unit testing. Understand the use of its three overloaded versions, including message suppliers for lazy evaluation, through practical testing scenarios on a StringUtils class.

assertNotNull() method

Assertions API provide static assertNotNull() method. This method helps us in validating that particular object is not null. This method takes the actual value and checks whether it is null or not.

  • If the actual value is not null then test case will pass.
  • If the actual value is null then test case will fail.

There are basically three overloaded methods for assertNotNull:-

Java
public static void assertNotNull(Object actual)
public static void assertNotNull(Object actual, String message)
public static void assertNotNull(Object actual, Supplier<String> messageSupplier)
  1. assertNotNull(Object actual) - It assert whether actual value is not null.

  2. assertNotNull(Object actual, String message) - It assert whether actual value is not null. In case, if the actual value is null then test case will fail with the provided message.

  3. assertNotNull(Object actual, Supplier<String> messageSupplier) - It assert whether actual value is not null. In case, if the actual value is null then test case will fail with the provided message through ...