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' ?
The assertThat is one of the JUnit methods from the Assert object that can be used to check if a specific value match to an expected one. It primarily accepts 2 parameters. First one if the actual value and the second is a matcher object.
StringUtils. containsIgnoreCase("Runaway train", "train")); Assert. assertTrue(StringUtils. containsIgnoreCase("Runaway train", "Train")); We can see that it will check if a substring is contained in a String, ignoring the case.
You should always use . equals() when comparing Strings in Java. JUnit calls the . equals() method to determine equality in the method assertEquals(Object o1, Object o2) .
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");
Use AllOf
Assert.assertThat(test, CoreMatchers.allOf(
containsString("foo"),
containsString("bar"),
containsString("bar2"),
containsString("ba3")));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With