Welcome 微信登录

首页 / 软件开发 / JAVA / JUnit 4新断言:Hamcrest的常用方法

JUnit 4新断言:Hamcrest的常用方法2014-06-02 csdn博客 wangjunjun2008一、字符相关匹配符

1、equalTo:

assertThat(testedValue, equalTo(expectedValue));

断言被测的testedValue等于expectedValue,equalTo可以断言数值之间,字符串之间和对象之间是否 相等,相当于Object的equals方法

2、equalToIgnoringCase:

assertThat(testedString, equalToIgnoringCase(expectedString));

断言被测的字符串testedString在忽略大小写的情况下等于expectedString

3、 equalToIgnoringWhiteSpace:

assertThat(testedString, equalToIgnoringWhiteSpace(expectedString);

断言被测的字符串testedString在忽略头尾的任意个空格的情况下等于expectedString

(注意:字符串中的空格不能被忽略)

4、containsString:

assertThat(testedString, containsString(subString) );

断言被测的字符串testedString包含子字符串subString

5、endsWith:

assertThat(testedString, endsWith(suffix));

断言被测的字符串testedString以子字符串suffix结尾

6、startsWith:

assertThat(testedString, startsWith(prefix));

断言被测的字符串testedString以子字符串prefix开始

二、一般匹配符

1、nullValue():

assertThat(object,nullValue());

断言被测object的值为null*/

2、notNullValue():

assertThat(object,notNullValue());

断言被测object的值不为null*/

3、is:

assertThat(testedString, is(equalTo(expectedValue)));

断言被测的object等于后面给出匹配表达式

1)is匹配符简写应用之一:

assertThat(testedValue, is(expectedValue));

is(equalTo(x))的简写,断言testedValue等于expectedValue

2)is匹配符简写应用之二:

assertThat(testedObject, is(Cheddar.class));

is(instanceOf(SomeClass.class))的简写,断言testedObject为Cheddar的实例

4、not:

assertThat(testedString, not(expectedString));

与is匹配符正好相反,断言被测的object不等于后面给出的object