How to assertThat something is null with Hamcrest?

user2811419 picture user2811419 · Sep 24, 2013 · Viewed 86.8k times · Source

How would I assertThat something is null?

for example

 assertThat(attr.getValue(), is(""));

But I get an error saying that I cannot have null in is(null).

Answer

Rohit Jain picture Rohit Jain · Sep 24, 2013

You can use IsNull.nullValue() method:

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

assertThat(attr.getValue(), is(nullValue()));