junit 斷言assertThat使用

一般匹配符
1、assertThat( testedNumber, allOf( greaterThan(8), lessThan(16) ));

註釋: allOf匹配符表明如果接下來的所有條件必須都成立測試才通過,相當於“與”(&&)

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

註釋:anyOf匹配符表明如果接下來的所有條件只要有一個成立則測試通過,相當於“或”(||)
3、assertThat( testedNumber, anything() );

註釋:anything匹配符表明無論什麼條件,永遠爲true
4、assertThat( testedString, is( "developerWorks" ) );

註釋: is匹配符表明如果前面待測的object等於後面給出的object,則測試通過
5、assertThat( testedString, not( "developerWorks" ) );

註釋:not匹配符和is匹配符正好相反,表明如果前面待測的object不等於後面給出的object,則測試通過

字符串相關匹配符

1、assertThat( testedString, containsString( "developerWorks" ));

註釋:containsString匹配符表明如果測試的字符串testedString包含子字符串"developerWorks"則測試通過

2、assertThat( testedString, endsWith( "developerWorks" ));

註釋:endsWith匹配符表明如果測試的字符串testedString以子字符串"developerWorks"結尾則測試通過

3、assertThat( testedString, startsWith( "developerWorks" ));

註釋:startsWith匹配符表明如果測試的字符串testedString以子字符串"developerWorks"開始則測試通過

4、assertThat( testedValue, equalTo( expectedValue ));

註釋: equalTo匹配符表明如果測試的testedValue等於expectedValue則測試通過,equalTo可以測試數值之間,字
符串之間和對象之間是否相等,相當於Object的equals方法

5、assertThat( testedString, equalToIgnoringCase( "developerWorks" ));

註釋:equalToIgnoringCase匹配符表明如果測試的字符串testedString在忽略大小寫的情況下等於"developerWorks"則測試通過

6、assertThat( testedString, equalToIgnoringWhiteSpace("developerWorks" ) );

註釋:equalToIgnoringWhiteSpace匹配符表明如果測試的字符串testedString在忽略頭尾的任意個空格的情況下等

於"developerWorks"則測試通過,注意:字符串中的空格不能被忽略


數值相關匹配符


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

註釋:closeTo匹配符表明如果所測試的浮點型數testedDouble在20.0±0.5範圍之內則測試通過

2、assertThat( testedNumber, greaterThan(16.0) );

註釋:greaterThan匹配符表明如果所測試的數值testedNumber大於16.0則測試通過

3、assertThat( testedNumber, lessThan (16.0) );

註釋:lessThan匹配符表明如果所測試的數值testedNumber小於16.0則測試通過

4、assertThat( testedNumber, greaterThanOrEqualTo (16.0) );

註釋: greaterThanOrEqualTo匹配符表明如果所測試的數值testedNumber大於等於16.0則測試通過

5、assertThat( testedNumber, lessThanOrEqualTo (16.0) );

註釋:lessThanOrEqualTo匹配符表明如果所測試的數值testedNumber小於等於16.0則測試通過


collection相關匹配符


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

註釋:hasEntry匹配符表明如果測試的Map對象mapObject含有一個鍵值爲"key"對應元素值爲"value"的Entry項則測試通過

2、assertThat( iterableObject, hasItem ( "element" ) );

註釋:hasItem匹配符表明如果測試的迭代對象iterableObject含有元素“element”項則測試通過

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

註釋: hasKey匹配符表明如果測試的Map對象mapObject含有鍵值“key”則測試通過

4、assertThat( mapObject, hasValue ( "key" ) );

註釋:hasValue匹配符表明如果測試的Map對象mapObject含有元素值“value”則測試通過


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章