簡體   English   中英

如何用 Hamcrest 斷言某些東西是空的?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM