[英]How to assertThat something is null with Hamcrest?
我將如何assertThat
something is 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()));
在 Kotlin is
是保留的所以使用:
assertThat(attr.getValue(), `is`(nullValue()));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.