如何断言某个字符串至少包含列表 <String> 中的一个值?

How to assert that some String contains at least one value from the List <String>?

我正在使用 Java 和 AssertJ 测试一些 UI 功能。 因此,当我从 UI 收到大量字符串时,我应该验证该字符串是否至少包含一个来自 List<String> 的预定义值。 做相反的事情很容易——验证列表是否至少包含一次字符串值,但这不是我的情况。 我找不到标准方法的解决方案。

public static final List<String> OPTIONS = Arrays.asList("Foo", "Bar", "Baz");

String text = "Just some random text with bar";

我需要的是这样的:

Assertions.assertThat(text)
                .as("Should contain at least one value from OPTIONS ")
                .containsAnyOf(OPTIONS)
.matches(s -> OPTIONS.stream().anyMatch(option -> s.contains(option)));

您还可以尝试使用 Condition 和 AssertJ 断言,例如 areAtLeastOne()、areAtLeast(),例如:

assertThat(OPTIONS)
.areAtLeastOne(new Condition<>(text::contains,
                                String.format("Error message '%s'", args);

另一个选项使用 AssertJ

assertThat(text.split(" ")).containsAnyElementsOf(OPTIONS);