How to assertThat something is null with Hamcrest? How to assertThat something is null with Hamcrest? java java

How to assertThat something is null with Hamcrest?


You can use IsNull.nullValue() method:

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


why not use assertNull(object) / assertNotNull(object) ?


If you want to hamcrest, you can do

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

In Junit you can do

import static junit.framework.Assert.assertNull;assertNull(object);