[Java]assertThat详解
2011-07-05 20:34 Firefly727 阅读(5314) 评论(0) 编辑 收藏 举报assertThat详解 ---来自http://yingfangming.blog.163.com/blog/static/165802470201092964526324/
㈠ 一般匹配符
1、allOf
匹配符表明如果接下来的所有条件必须都成立测试才通过,相当于“与”(&&)
assertThat( testedNumber, allOf( greaterThan(8), lessThan(16) ) );
2、anyOf
匹配符表明如果接下来的所有条件只要有一个成立则测试通过,相当于“或”(||)
assertThat( testedNumber, anyOf( greaterThan(16), lessThan(8) ) );
3、anything
匹配符表明无论什么条件,永远为true
assertThat( testedNumber, anything() );
4、is
匹配符表明如果前面待测的object等于后面给出的object,则测试通过
assertThat( testedString, is( "developerWorks" ) );
5、not
匹配符和is匹配符正好相反,表明如果前面待测的object不等于后面给出的object,则测试通过
assertThat( testedString, not( "developerWorks" ) );
㈡ 字符串相关匹配符
1、containsString
匹配符表明如果测试的字符串testedString 包含 子字符串"developerWorks"则测试通过
assertThat( testedString, containsString( "developerWorks" ) );
2、endsWith
匹配符表明如果测试的字符串testedString以子字符串"developerWorks"结尾则测试通过
assertThat( testedString, endsWith( "developerWorks" ) );
3、startsWith
匹配符表明如果测试的字符串testedString以子字符串"developerWorks"开始则测试通过
assertThat( testedString, startsWith( "developerWorks" ) );
4、equalTo
匹配符表明如果测试的testedValue等于expectedValue则测试通过,equalTo可以测试数值之间,字符串
之间和对象之间是否相等,相当于Object的equals方法
assertThat( testedValue, equalTo( expectedValue ) );
5、equalToIgnoringCase
匹配符表明如果测试的字符串testedString在忽略大小写的情况下等于"developerWorks"则测试通过
assertThat( testedString, equalToIgnoringCase( "developerWorks" ) );
6、equalToIgnoringWhiteSpace
匹配符表明如果测试的字符串testedString在忽略头尾的任意个空格的情况下等于"developerWorks"则
测试通过,注意:字符串中的空格不能被忽略
assertThat( testedString, equalToIgnoringWhiteSpace( "developerWorks" ) );
㈢ 数值相关匹配符
1、closeTo
匹配符表明如果所测试的浮点型数testedDouble在20.0±0.5范围之内则测试通过
assertThat( testedDouble, closeTo( 20.0, 0.5 ) );
2、greaterThan
匹配符表明如果所测试的数值testedNumber大于16.0则测试通过
assertThat( testedNumber, greaterThan(16.0) );
3、lessThan
匹配符表明如果所测试的数值testedNumber小于16.0则测试通过
assertThat( testedNumber, lessThan (16.0) );
4、greaterThanOrEqualTo
匹配符表明如果所测试的数值testedNumber大于等于16.0则测试通过
assertThat( testedNumber, greaterThanOrEqualTo (16.0) );
5、lessThanOrEqualTo
匹配符表明如果所测试的数值testedNumber小于等于16.0则测试通过
assertThat( testedNumber, lessThanOrEqualTo (16.0) );
㈣ collection相关匹配符
1、hasEntry
匹配符表明如果测试的Map对象mapObject含有一个键值为"key"对应元素值为"value"的Entry项则测试通过
assertThat( mapObject, hasEntry( "key", "value" ) );
2、hasItem
匹配符表明如果测试的迭代对象iterableObject含有元素“element”项则测试通过
assertThat( iterableObject, hasItem ( "element" ) );
3、hasKey
匹配符表明如果测试的Map对象mapObject含有键值“key”则测试通过
assertThat( mapObject, hasKey ( "key" ) );
4、hasValue
匹配符表明如果测试的Map对象mapObject含有元素值“value”则测试通过
assertThat( mapObject, hasValue ( "key" ) );