一、字符相關匹配符
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
查看本欄目