我想知道是否有人知道使用 assertThat()
和 Matchers
检查列表是否为空的方法?
我能看到的最好的方法就是使用 JUnit:
assertFalse(list.isEmpty());
但我希望在 Hamcrest 有某种方法可以做到这一点。
最佳答案
总会有的
assertThat(list.isEmpty(), is(false));
...但我猜这不是你的意思:)
或者:
assertThat((Collection)list, is(not(empty())));
empty()
是 Matchers
类中的静态变量。请注意需要将 list
转换为 Collection
,这要归功于 Hamcrest 1.2 的不稳定泛型。
以下导入可用于 hamcrest 1.3
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNot.*;
关于java - 检查 Hamcrest 中的 List 是否为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3631110/