Assert包含jUnit中的字符串 [英] AssertContains on strings in jUnit
本文介绍了Assert包含jUnit中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有更好的方法在jUnit中写入
Is there a nicer way to write in jUnit
String x = "foo bar";
Assert.assertTrue(x.contains("foo"));
推荐答案
如果添加Hamcrest和JUnit4,你可以做:
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"));
所需的静态导入是:
import static org.junit.Assert.assertThat;
import static org.hamcrest.CoreMatchers.containsString;
这篇关于Assert包含jUnit中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文