首页 > 解决方案 > AssertJ:一个集合包含一个值以字符串结尾的元素

问题描述

我想检查被测集合是否包含任何以特定字符串结尾的元素。

使用 Hamcrest 可能是这样的:

assertThat("Contains an element ending with 'xyz'", 
               myCollection, hasItems(endsWith("xyz")));

如何对 AssertJ 做同样的事情?

标签: javaassertj

解决方案


您可以使用anyMatch(Predicate)or anySatisfy(assertions on elements),例如:

assertThat(myCollection).as("Contains an element ending with 'xyz'") 
                        .anyMatch(element -> element.endsWith("xyz"));

推荐阅读