Want a JUnitMatchers AssertThat to test string contains 3 or more sub strings (currently using assertThat ... both ... and ....)

k1eran picture k1eran · Jul 1, 2013 · Viewed 8.1k times · Source
import static org.junit.matchers.JUnitMatchers.both;
import static org.junit.matchers.JUnitMatchers.containsString;

Now I check it contains foo and bar as below ...

        Assert.assertThat(text,
            both(containsString("foo")).
            and(containsString("bar")));

What is cleanest way to test also check it contains 3 or more strings e.g. 'foo', 'bar' and 'baz' ?

Answer

John B picture John B · Jul 1, 2013

Use AllOf

 Assert.assertThat(test, CoreMatchers.allOf(
      containsString("foo"),
      containsString("bar"),
      containsString("bar2"),
      containsString("ba3")));