ripper234 ripper234 - 1 year ago 142
Java Question

AssertContains on strings in jUnit

Is there a nicer way to write in jUnit

String x = "foo bar";


If you add in Hamcrest and JUnit4, you could do:

String x = "foo bar";
Assert.assertThat(x, CoreMatchers.containsString("foo"));

With some static imports, it looks a lot better:

assertThat(x, containsString("foo"));

The static imports needed would be:

import static org.junit.Assert.assertThat;
import static org.hamcrest.CoreMatchers.containsString;