检查 Hamcrest 中的列表是否为空 [英] Checking that a List is not empty in Hamcrest
本文介绍了检查 Hamcrest 中的列表是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道是否有人知道使用 assertThat()
和 Matchers
来检查 List 是否为空的方法?
I was wondering if anyone knew of a way to check if a List is empty using assertThat()
and Matchers
?
我认为最好的方法就是使用 JUnit:
Best way I could see just use JUnit:
assertFalse(list.isEmpty());
但我希望在 Hamcrest 有某种方法可以做到这一点.
But I was hoping that there was some way to do this in Hamcrest.
推荐答案
好吧
assertThat(list.isEmpty(), is(false));
...但我猜这不是你的意思:)
... but I'm guessing that's not quite what you meant :)
或者:
assertThat((Collection)list, is(not(empty())));
empty()
是 Matchers
类中的一个静态变量.请注意需要将 list
转换为 Collection
,这要归功于 Hamcrest 1.2 的古怪泛型.
empty()
is a static in the Matchers
class. Note the need to cast the list
to Collection
, thanks to Hamcrest 1.2's wonky generics.
以下导入可以与 hamcrest 1.3 一起使用
The following imports can be used with hamcrest 1.3
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNot.*;
这篇关于检查 Hamcrest 中的列表是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文