当前位置:天才代写 > tutorial > JAVA 教程 > JUnit 4新断言:Hamcrest的常用要领

JUnit 4新断言:Hamcrest的常用要领

2017-11-02 08:00 星期四 所属: JAVA 教程 浏览:57

副标题#e#

一、字符相关匹配符

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


#p#副标题#e#

5、allOf:

assertThat(testedNumber, allOf( greaterThan(8), lessThan(16) ) );

断言切合所有条件,相当于“与”(&&)

6、anyOf:

assertThat(testedNumber, anyOf( greaterThan(16), lessThan(8) ) );

断言切合条件之一,相当于“或”(||)

三、数值相关匹配符

1、closeTo:

assertThat(testedDouble, closeTo( 20.0, 0.5 ));

断言被测的浮点型数testedDouble在20.0-0.5 ~ 20.0+0.5范畴之内

2、greaterThan:

assertThat(testedNumber, greaterThan(16.0));

断言被测的数值testedNumber大于16.0

3、lessThan:

assertThat(testedNumber, lessThan (16.0));

断言被测的数值testedNumber小于16.0

4、greaterThanOrEqualTo:

assertThat(testedNumber, greaterThanOrEqualTo (16.0));

断言被测的数值testedNumber大于便是16.0

5、lessThanOrEqualTo:

assertThat(testedNumber, lessThanOrEqualTo (16.0));

断言被测的testedNumber小于便是16.0

四、荟萃相关匹配符

1、hasEntry:

assertThat(mapObject, hasEntry("key", "value" ) );

断言被测的Map工具mapObject含有一个键值为"key"对应元素值为"value"的 Entry项

2、hasItem:

assertThat(iterableObject, hasItem (element));

表白被测的迭代工具iterableObject含有元素element项则测试通过

3、hasKey:

assertThat(mapObject, hasKey ("key"));

断言被测的Map工具mapObject含有键值“key”

4、hasValue:

assertThat(mapObject, hasValue(value));

断言被测的Map工具mapObject含有元素值value

查察本栏目

 

    关键字:


天才代写-代写联系方式