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

StackOverflow https://stackoverflow.com/questions/17403877

  •  02-06-2022
  •  | 
  •  

Question

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' ?

Was it helpful?

Solution

Use AllOf

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

OTHER TIPS

I don't know a elegant way in pure JUnit but you could take a look at Fixtures for Easy Software Testing

I'm using it for quite some time and it makes like so much easier.

assertThat(text).contains("foo").contains("bar");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top