1.JUnit4.4引入了Hamcrest框架,Hamcest提供了一套匹配符Matcher,这些匹配符更接近自然语言,可读性高,更加灵活;
2.使用全新的断言语法:assertThat,结合Hamcest提供的匹配符,只用这一个方法,就可以实现所有的测试;
3.assertThat语法如下:
assertThat(T actual, Matcher matcher);
assertThat(String reason, T actual, Matcher matcher); 其中actual为需要测试的变量,matcher为使用Hamcrest的匹配符来表达变量actual期望值的声明;
4.注意事项:
a.必须导入JUnit4.4之后的版本以及hamcrest-core.jar和hamcrest-library.jar才能使用assertThat方法; b.不需要继承TestCase类,但是需要测试方法前必须加“@Test”。
hamcrest-core.jar
hamcrest-library.jar
代码:
@Test
public void array(){
boolean flag = true;
assertThat(“ddddddddd”, flag, is(true));
}