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
  •  | 
  •  

Pregunta

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

¿Fue útil?

Solución

Use AllOf

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

Otros consejos

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");
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top