Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Way to Assert That Each String in a Collection Contains a Substring?

What is the best way to assert that each element in a collection of strings contains a particular substring?

Something like

List<String> list = Arrays.asList("xyfoobar", "foobarxy", "xyfoobarxy");

assertThat(list, eachElementContains("foobar")); // pass
like image 374
Marteng Avatar asked Nov 14 '25 18:11

Marteng


1 Answers

Something simple like:

list.forEach(string -> {
    assertTrue(string.contains("foobar"));
});

This doesn't use Hamcrest matchers but has the same semantics.

like image 199
cameron1024 Avatar answered Nov 17 '25 08:11

cameron1024



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!