我如何使用assertThat
来判断某个值是否为null
?
例如:
assertThat(attr.getValue(), is(""));
但我遇到了一个错误,说我不能在 is(null)
中使用 null
。
IsNull.nullValue()
方法:import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
assertThat(attr.getValue(), is(nullValue()));
assertNull(object)
/ assertNotNull(object)
?hamcrest
,可以这样做。import static org.hamcrest.Matchers.nullValue;
assertThat(attr.getValue(), is(nullValue()));
在 Junit
中,您可以执行
import static junit.framework.Assert.assertNull;
assertNull(object);
使用以下内容(来自 Hamcrest):
assertThat(attr.getValue(), is(nullValue()));
is
是保留的关键字,因此请使用以下语法:assertThat(attr.getValue(), `is`(nullValue()));
is
? - VIN
IsNull
类,它是一个静态方法。只需使用static import
或者使用IsNull.nullValue()
即可。 - Rohit Jainimport static org.hamcrest.core.IsNull.nullValue;
添加到你的类中。 - Rohit Jainimport static org.hamcrest.CoreMatchers.nullValue
。 - ThomasW