AssertContains on strings in jUnit

前端 未结 10 1688
清酒与你
清酒与你 2020-12-22 21:09

Is there a nicer way to write in jUnit

String x = \"foo bar\";
Assert.assertTrue(x.contains(\"foo\"));
10条回答
  •  无人及你
    2020-12-22 22:00

    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.hamcrest.MatcherAssert.assertThat;
    import static org.hamcrest.CoreMatchers.containsString;
    

提交回复
热议问题