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”则测试通过
-------------------------------------------------------------------------------------------------------------------------------
JUnit4.4引入了Hamcrest框架,Hamcest提供了一套匹配符Matcher,这些匹配符更接近自然语言,可读性高,更加灵活
/**equalTo匹配符断言被测的testedValue等于expectedValue,
* equalTo可以断言数值之间,字符串之间和对象之间是否相等,相当于Object的equals方法
*/
assertThat(testedValue, equalTo(expectedValue));
/**equalToIgnoringCase匹配符断言被测的字符串testedString
*在忽略大小写的情况下等于expectedString
*/
assertThat(testedString, equalToIgnoringCase(expectedString));
/**equalToIgnoringWhiteSpace匹配符断言被测的字符串testedString
*在忽略头尾的任意个空格的情况下等于expectedString,
*注意:字符串中的空格不能被忽略
*/
assertThat(testedString, equalToIgnoringWhiteSpace(expectedString);
/**containsString匹配符断言被测的字符串testedString包含子字符串subString**/
assertThat(testedString, containsString(subString) );
/**endsWith匹配符断言被测的字符串testedString以子字符串suffix结尾*/
assertThat(testedString, endsWith(suffix));
/**startsWith匹配符断言被测的字符串testedString以子字符串prefix开始*/
assertThat(testedString, startsWith(prefix));
/**nullValue()匹配符断言被测object的值为null*/
assertThat(object,nullValue());
/**notNullValue()匹配符断言被测object的值不为null*/
assertThat(object,notNullValue());
/**is匹配符断言被测的object等于后面给出匹配表达式*/
assertThat(testedString, is(equalTo(expectedValue)));
/**is匹配符简写应用之一,is(equalTo(x))的简写,断言testedValue等于expectedValue*/
assertThat(testedValue, is(expectedValue));
/**is匹配符简写应用之二,is(instanceOf(SomeClass.class))的简写,
*断言testedObject为Cheddar的实例
*/
assertThat(testedObject, is(Cheddar.class));
/**not匹配符和is匹配符正好相反,断言被测的object不等于后面给出的object*/
assertThat(testedString, not(expectedString));
/**allOf匹配符断言符合所有条件,相当于“与”(&&)*/
assertThat(testedNumber, allOf( greaterThan(8), lessThan(16) ) );
/**anyOf匹配符断言符合条件之一,相当于“或”(||)*/
assertThat(testedNumber, anyOf( greaterThan(16), lessThan(8) ) );
/**closeTo匹配符断言被测的浮点型数testedDouble在20.0¡À0.5范围之内*/
assertThat(testedDouble, closeTo( 20.0, 0.5 ));
/**greaterThan匹配符断言被测的数值testedNumber大于16.0*/
assertThat(testedNumber, greaterThan(16.0));
/** lessThan匹配符断言被测的数值testedNumber小于16.0*/
assertThat(testedNumber, lessThan (16.0));
/** greaterThanOrEqualTo匹配符断言被测的数值testedNumber大于等于16.0*/
assertThat(testedNumber, greaterThanOrEqualTo (16.0));
/** lessThanOrEqualTo匹配符断言被测的testedNumber小于等于16.0*/
assertThat(testedNumber, lessThanOrEqualTo (16.0));
/**hasEntry匹配符断言被测的Map对象mapObject含有一个键值为"key"对应元素值为"value"的Entry项*/
assertThat(mapObject, hasEntry("key", "value" ) );
/**hasItem匹配符表明被测的迭代对象iterableObject含有元素element项则测试通过*/
assertThat(iterableObject, hasItem (element));
/** hasKey匹配符断言被测的Map对象mapObject含有键值“key”*/
assertThat(mapObject, hasKey ("key"));
/** hasValue匹配符断言被测的Map对象mapObject含有元素值value*/
assertThat(mapObject, hasValue(value));
---------------------------------------------------------------------------------------------------------------------------
- //Get number from [p, r]
- int getRandomFrom(int p, int r){
- int chooes = r-p+1;
- return (int) Math.floor(Math.random() * chooes + p);
- }
- import static org.junit.Assert.*;
- import static org.hamcrest.CoreMatchers.*;
- import static org.junit.matchers.JUnitMatchers.*;
- @Test
- public void testGetRandomFrom(){
- for(int i=0 ; i<100 ; i++){
- assertThat(quickSort.getRandomFrom(5, 10), anyOf(equalTo(5),equalTo(6),equalTo(7),equalTo(8),equalTo(9),equalTo(10)));
- }
- }
2.使用全新的断言语法:assertThat,结合Hamcest提供的匹配符,只用这一个方法,就可以实现所有的测试;
3.assertThat语法如下:
assertThat(T actual, Matcher<T> matcher);
assertThat(String reason, T actual, Matcher<T> matcher);
其中actual为需要测试的变量,matcher为使用Hamcrest的匹配符来表达变量actual期望值的声明;
4.注意事项:
a.必须导入JUnit4.4之后的版本才能使用assertThat方法;
b.不需要继承TestCase类,但是需要测试方法前必须加“@Test”。
实例:
- public class C {
- public int add(int a, int b) {
- return a + b;
- }
- public double div(double a, double b) {
- return a / b;
- }
- public String getName(String name) {
- return name;
- }
- public List<String> getList(String item) {
- List<String> l = new ArrayList<String>();
- l.add(item);
- return l;
- }
- public Map<String, String> getMap(String key, String value) {
- Map<String, String> m = new HashMap<String, String>();
- m.put(key, value);
- return m;
- }
- }
- import static org.junit.Assert.*;
- import static org.hamcrest.Matchers.*;
- import java.util.List;
- import java.util.Map;
- import org.junit.Test;
- public class CTest {
- @Test
- public void testAdd() {
- //一般匹配符
- int s = new C().add(1, 1);
- //allOf:所有条件必须都成立,测试才通过
- assertThat(s, allOf(greaterThan(1), lessThan(3)));
- //anyOf:只要有一个条件成立,测试就通过
- assertThat(s, anyOf(greaterThan(1), lessThan(1)));
- //anything:无论什么条件,测试都通过
- assertThat(s, anything());
- //is:变量的值等于指定值时,测试通过
- assertThat(s, is(2));
- //not:和is相反,变量的值不等于指定值时,测试通过
- assertThat(s, not(1));
- //数值匹配符
- double d = new C().div(10, 3);
- //closeTo:浮点型变量的值在3.0±0.5范围内,测试通过
- assertThat(d, closeTo(3.0, 0.5));
- //greaterThan:变量的值大于指定值时,测试通过
- assertThat(d, greaterThan(3.0));
- //lessThan:变量的值小于指定值时,测试通过
- assertThat(d, lessThan(3.5));
- //greaterThanOrEuqalTo:变量的值大于等于指定值时,测试通过
- assertThat(d, greaterThanOrEqualTo(3.3));
- //lessThanOrEqualTo:变量的值小于等于指定值时,测试通过
- assertThat(d, lessThanOrEqualTo(3.4));
- //字符串匹配符
- String n = new C().getName("Magci");
- //containsString:字符串变量中包含指定字符串时,测试通过
- assertThat(n, containsString("ci"));
- //startsWith:字符串变量以指定字符串开头时,测试通过
- assertThat(n, startsWith("Ma"));
- //endsWith:字符串变量以指定字符串结尾时,测试通过
- assertThat(n, endsWith("i"));
- //euqalTo:字符串变量等于指定字符串时,测试通过
- assertThat(n, equalTo("Magci"));
- //equalToIgnoringCase:字符串变量在忽略大小写的情况下等于指定字符串时,测试通过
- assertThat(n, equalToIgnoringCase("magci"));
- //equalToIgnoringWhiteSpace:字符串变量在忽略头尾任意空格的情况下等于指定字符串时,测试通过
- assertThat(n, equalToIgnoringWhiteSpace(" Magci "));
- //集合匹配符
- List<String> l = new C().getList("Magci");
- //hasItem:Iterable变量中含有指定元素时,测试通过
- assertThat(l, hasItem("Magci"));
- Map<String, String> m = new C().getMap("mgc", "Magci");
- //hasEntry:Map变量中含有指定键值对时,测试通过
- assertThat(m, hasEntry("mgc", "Magci"));
- //hasKey:Map变量中含有指定键时,测试通过
- assertThat(m, hasKey("mgc"));
- //hasValue:Map变量中含有指定值时,测试通过
- assertThat(m, hasValue("Magci"));
- }
- }