1. Overview

When we write unit tests in Java, particularly with the JUnit framework, we often need to verify that certain variables are null or not null. Hamcrest, a popular library of Matchers for creating flexible tests, provides a convenient way to achieve this.

In this quick tutorial, we’ll explore how to check if a variable is null or non-null using JUnit and Hamcrest’s Matchers.

2. Using Hamcrest’s assertThat()

To use Hamcrest, we need to add the dependency into the pom.xml:

<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest</artifactId>
    <version>2.2</version>
    <scope>test</scope>
</dependency>

We can check the latest version in Maven Central.

Hamcrest’s assertThat() method and its Matchers allow us to write flexible assertions. Next, let’s have a look at how to assert a variable is or isn’t null using this approach.

Hamcrest offers null-related matcher methods in the org.hamcrest.core.IsNull class. *For example, we can use the static methods nullValue() and notNullValue() to get the Matchers for null and non-null checks.

Also, Hamcrest groups commonly used Matchers in the org.hamcrest.Matchers class. In the Matchers class, nullValue() and notNullValue() methods are available as well. They simply call the corresponding methods in the IsNull class. Therefore, we can import static the methods from either class to use these methods and make the code easy to read:

import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;

We can use them like so:

String theNull = null;
assertThat(theNull, nullValue());
 
String theNotNull = "I am a good String";
assertThat(theNotNull, notNullValue());

For a non-null check, we can also combine Matchers and use not(nullValue()) as an alternative:

assertThat(theNotNull, not(nullValue()));

It’s worth noting that the not() method is from the org.hamcrest.core.IsNot class and it negates the match parameter’s logic.

3. Using JUnit’s null and Non-Null Assertions

As we’ve seen, Hamcrest’s matchers are convenient for performing null or non-null assertions. Alternatively, the assertNull() and assertNotNull() methods shipped with JUnit are straightforward to do these checks:

String theNull = null;
assertNull(theNull); 
 
String theNotNull = "I am a good String";
assertNotNull(theNotNull);

As the code shows, even without Matchers, the JUnit assertion methods remain easy to use and read when checking for nulls.

4. Conclusion

In this quick article, we’ve explored different ways to assert null and non-null variables effectively using JUnit and Hamcrest.

By using Hamcrest’s nullValue() and notNullValue() Matchers, we can easily check if variables are null or non-null in our unit tests. The library’s expressive syntax makes our tests more readable and maintainable.

Alternatively, JUnit’s standard assertNull() and assertNotNull() assertions are straightforward in doing this job.

As always, the complete source code for the examples is available over on GitHub.