Java 如何用 Hamcrest 断言某些东西是空的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18987692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:07:11  来源:igfitidea点击:

How to assertThat something is null with Hamcrest?

javaasserthamcrest

提问by user2811419

How would I assertThatsomething is null?

我怎么会有assertThat东西null

for example

例如

 assertThat(attr.getValue(), is(""));

But I get an error saying that I cannot have nullin is(null).

但是我收到一个错误,说我不能nullis(null).

采纳答案by Rohit Jain

You can use IsNull.nullValue()method:

您可以使用IsNull.nullValue()方法:

import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;

assertThat(attr.getValue(), is(nullValue()));

回答by Chetya

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

为什么不使用assertNull(object)/ assertNotNull(object)

回答by blackpanther

Use the following (from Hamcrest):

使用以下内容(来自 Hamcrest):

assertThat(attr.getValue(), is(nullValue()));

In Kotlin isis reserved so use:

在 Kotlin 中is是保留的所以使用:

assertThat(attr.getValue(), `is`(nullValue()));

回答by Sajan Chandran

If you want to hamcrest, you can do

如果你愿意hamcrest,你可以

import static org.hamcrest.Matchers.nullValue;

assertThat(attr.getValue(), is(nullValue()));

In Junityou can do

Junit你可以做

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