7

I'm testing some UI functionality with Java and AssertJ. So when I receive some massive string from UI, I should verify if that String contains at least one predefined value from List<String>. It is easy to do opposite thing - verify if list contains at least once some String value but this is not my case. I can't find solution in standard methods.

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

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

what I need is smth like this :

Assertions.assertThat(text)
                .as("Should contain at least one value from OPTIONS ")
                .containsAnyOf(OPTIONS)
Vadam
  • 85
  • 1
  • 4

4 Answers4

7
.matches(s -> OPTIONS.stream().anyMatch(option -> s.contains(option)));
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
3

You can also try to use Condition and AssertJ assertions like areAtLeastOne(), areAtLeast(), for instance:

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

Another option using AssertJ

assertThat(text.split(" ")).containsAnyElementsOf(OPTIONS);
Olivier Tonglet
  • 3,312
  • 24
  • 40
0

Another simplified option using AssertJ:

.matches(s-> Stream.of("Foo", "Bar", "Baz").anyMatch(s::contains));
K Are
  • 1
  • 1