繁体   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