How to assertThat something is null with Hamcrest?

跟風遠走 提交于 2019-12-20 09:07:29

问题


How would I assertThat something is null?

for example

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

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


回答1:


You can use IsNull.nullValue() method:

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

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



回答2:


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




回答3:


If you want to hamcrest, you can do

import static org.hamcrest.Matchers.nullValue;

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

In Junit you can do

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



回答4:


Use the following (from Hamcrest):

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


来源:https://stackoverflow.com/questions/18987692/how-to-assertthat-something-is-null-with-hamcrest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!