JUnit assertNull example

Junit 5’s org.junit.jupiter.Assertions class provides different static assertions method to write test cases.

Please note that you need to use JUnit’s org.junit.Assert class in case of JUnit 4 or JUnit 3 to assert using assertNull method.

Assertions.assertNull() checks that object is null. In case, object is not null, it will through AssertError.

public static void assertNull(Object actual)
public static void assertNull(Object actual, String message)
public static void assertNull(Object actual, Supplier messageSupplier)

Here is simple example

When you run above testcase, you will get below output:
AssertNullTestOutput
Let’s understand output of each testcase:


testIndia – Pass

As countryCapitalMap does not contain key “India”, assertNull will return true and this testcase will pass.


testUSA – Pass

As countryCapitalMap does not contain key “USA”, assertNull will return true and this testcase will pass.


testJapan – Fail

As countryCapitalMap contains key “Japan”, assertNull will return false and this testcase will fail.


That’s all about Assertions.assertNull() method.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *