AssertContains on strings in jUnit

前端 未结 10 1637
清酒与你
清酒与你 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:09

    You can use assertj-fluent assertions. It has lot of capabilities to write assertions in more human readable - user friendly manner.

    In your case, it would be

     String x = "foo bar";
     assertThat(x).contains("foo");
    

    It is not only for the strings, it can be used to assert lists, collections etc.. in a friendlier way

提交回复
热议问题