java - 检查 Hamcrest 中的 List 是否为空

标签 java collections junit hamcrest

我想知道是否有人知道使用 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/

相关文章:

java - 泛型继承 + 集合 + 方法 = 问题

java - 用于测试来自数据库的数据的 JUnit 测试用例是最新的而不是过时的

java - 如何在java中使用FastDateFormat来查找当前日期是否在给定日期之间?

java - 单击按钮时更改 javafx webview 方向

java - Collections.sort 方法有时会在多线程环境中抛出 ConcurrentModificationException。列表未在结构上进行修改

java - 使用 JMockit 模拟

java - 围绕 Sytem.in 和 System.out 进行 JUnit 测试

java - Maven 执行插件 : How configure the working directory

java - 元素到 HTMLDocument 中的字符串

Java 结合自定义比较器使用比较器