junit4 断言方法

一、字符相关匹配符
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
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

源:http://houmaolin0521.blog.163.com/blog/static/11335164920121018101658230/ 

相关推荐