javaasserthamcrest

How to assertThat something is null with Hamcrest?


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).


Solution

  • You can use IsNull.nullValue() method:

    import static org.hamcrest.Matchers.is;
    import static org.hamcrest.core.IsNull.nullValue;
    
    
    assertThat(attr.getValue(), is(nullValue()));