Explain Codes LogoExplain Codes Logo

How to assertThat something is null with Hamcrest?

java
assertions
hamcrest
testing
Anton ShumikhinbyAnton Shumikhin·Oct 3, 2024
TLDR

Quickly make a null assertion using Hamcrest's assertThat method combined with nullValue(). You get a short yet powerful check:

import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.nullValue; // When you hope for nothing! assertThat(myObject, nullValue());

Asserting null gracefully

Before we plan our white flag celebration 🏳️ with assertThat(myObject, nullValue()), let's better understand and skillfully apply Hamcrest's features for null assertions.

Graceful null checking strategies

With Hamcrest, our tests become more expressive and easier to read. It's like Shakespeare started writing code! 🎭

JUnit vs Hamcrest in null assertion

While JUnit's assertNull provides a minimalist check:

import static org.junit.Assert.assertNull; // Even null challenges can't mess with us! assertNull(myObject);

Hamcrest's nullValue() brings the language of testing to life:

// It's like expecting an empty box assertThat(myObject, is(nullValue()));

The keyword is teamed with nullValue(), gives our assertion a bit of English sauce!

The journey to master nullValue()

nullValue() is a card-carrying member of Hamcrest's core matchers. It gives your test cases better semantics and shifts them into BDD territory!

On the other side: asserting non-null

What's the opposite of null? Well, not-null, obviously! And how does Hamcrest handle this?

import static org.hamcrest.Matchers.notNullValue; // Nothing says "I exist!" better than this! assertThat(myObject, is(notNullValue()));

Though it mirrors the null check, it signals that an object must not be null. Talk about stating the obvious!

Write your code like a bestselling author

Mastering Hamcrest goes beyond just checking if something is lazily lounging around as null. Let's look at a scenario for clarity:

List<String> myList = getItems(); // JUnit way - no frills assertTrue(myList.isEmpty()); // Hamcrest way - little thrills assertThat(myList, is(empty()));

The Hamcrest approach reveals the intent of the assertion, just like a detective novel! 🕵️

From your brain to your IDE

When you code, your choice of assertion tools can reveal subtle nuances about your intentions. So be mindful!

A whiff of Kotlin

For any Kotlin enthusiasts out there, Hamcrest will make you feel right at home:

import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.`is` import org.hamcrest.Matchers.nullValue val myVal: String? = null // Kotlin with Hamcrest: assertThat(myVal, `is`(nullValue()))

Code snippets are your vocabulary

Remember your language classes? Codes are like words - they need the right format for clarity. So, stick to <code> tags while discussing to avoid messing with reserved words.

A serving of AssertJ

AssertJ alternates with Hamcrest to give a different flavor of fluent assertions:

import static org.assertj.core.api.Assertions.assertThat; List<String> myList = getItems(); // AssertJ turns us all to poets assertThat(myList).isEmpty();

Like Hamcrest, AssertJ adds a touch of natural language to our assertion, but with a different style.