有了assertj,我可以使用 anyMatch 测试集合是否至少有一个元素与 predicate 匹配,例如。
anyMatch
var list = List.of("abc", "xyz") assertThat(list).anyMatch(element -> element.endsWith("xyz"));
但如何测试集合中是否只有一个元素与 predicate 匹配?
8tntrjer1#
用过滤器怎么样?https://github.com/assertj/assertj-core/blob/9eceff23e5b019af3d09c3e9bbc58126d51c02b6/src/main/java/org/assertj/core/api/abstractiterableassert.java#l3283
var list = List.of("a", "b", "c!"); assertThat(list) .filteredOn(element -> element.endsWith("!")) .hasSize(1);
1条答案
按热度按时间8tntrjer1#
用过滤器怎么样?https://github.com/assertj/assertj-core/blob/9eceff23e5b019af3d09c3e9bbc58126d51c02b6/src/main/java/org/assertj/core/api/abstractiterableassert.java#l3283