AssertContains on strings in jUnit
有没有更好的方法来编写jUnit
String x = "foo bar";
Assert.assertTrue(x.contains("foo"));
有没有更好的方法来编写jUnit
String x = "foo bar";
Assert.assertTrue(x.contains("foo"));
如果您添加Hamcrest和JUnit4,则可以执行以下操作:
String x = "foo bar";
Assert.assertThat(x, CoreMatchers.containsString("foo"));
使用一些静态导入,它看起来好多了:
assertThat(x, containsString("foo"));
所需的静态导入将是:
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.containsString;