Hamcrest、Mockito 专题

UT中需要的jar

Junit4.1X.jar

hamcrest-library-1.x.jar

hamcrest-core-l.x.jar

mockito-all-1.10.x.jar

 

Junit 使用hamcrest的Matcher找不到的问题(比如greaterThan)

问题:
用junit在进行单元测试的时候,使用assertThat方法,发现hamcrest的greaterThan等方法无法识别【说是没有定义】,而且无法静态引用到Matchers类【import static org.hamcrest.Matchers.*;】
junit版本: 4.12
hamcrest版本:hamcrest-core-1.3

原因:
后来发现原因是因为junit默认只会依赖hamcrest-core-1.3包【我用maven构建的项目管理的jar包】,而实际上hamcrest还有一个hamcrest-library.jar用来放置很多工具

解决:
显式引入hamcrest-library-1.3.jar包


 

import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

public class HamcrestDemo {
    @Test
    public void testAdd() {
        int actual = 3;
        int expected = 3;
        // 一般匹配符
        assertEquals(expected, actual);
        // allOf:所有条件必须都成立,测试才通过
        assertThat(actual, allOf(greaterThan(1), lessThan(4)));
        // anyOf:只要有一个条件成立,测试就通过
        assertThat(actual, anyOf(greaterThan(5), lessThan(4)));
        // anything:无论什么条件,测试都通过
        assertThat(actual, anything());
        // is:变量的值等于指定值时,测试通过
        assertThat(actual, is(3));
        // not:和is相反,变量的值不等于指定值时,测试通过
        assertThat(actual, not(1));
    }


    @Test
    public void testDiv() {
        // 数值匹配符
        double divResult = 10.0 / 3;
        System.out.println(divResult);
        // closeTo:浮点型变量的值在3.0±0.5范围内,测试通过
        assertThat(divResult, closeTo(3.0, 0.5));
        // greaterThan:变量的值大于指定值时,测试通过
        assertThat(divResult, greaterThan(2.9));
        // lessThan:变量的值小于指定值时,测通过
        assertThat(divResult, lessThan(3.5));
        // greaterThanOrEqualTo:变量的值大于等于指定值时,测试通过
        assertThat(divResult, greaterThanOrEqualTo(3.3));
        // lessThanOrEqualTo:变量的值小于等于指定值时,测试通过
        assertThat(divResult, lessThanOrEqualTo(3.4));

    }

    @Test
    public void testGetName() {
        // 字符串匹配符
        String actual = "Magci";
        // containsString:字符串变量中包含指定字符串时,测试通过
        assertThat(actual, containsString("ci"));
        // startsWith:字符串变量以指定字符串开头时,测试通过
        assertThat(actual, startsWith("Ma"));
        // endsWith:字符串变量以指定字符串结尾时,测试通过
        assertThat(actual, endsWith("i"));
        // euqalTo:字符串变量等于指定字符串时,测试通过
        assertThat(actual, equalTo("Magci"));
        // equalToIgnoringCase:字符串变量在忽略大小写的情况下等于指定字符串时,测试通过
        assertThat(actual, equalToIgnoringCase("magci"));
        // equalToIgnoringWhiteSpace:字符串变量在忽略头尾任意空格的情况下等于指定字符串时,测试通过
        assertThat(actual, equalToIgnoringWhiteSpace(" Magci   "));

    }

    @Test
    public void testGetList() {
        // 集合匹配符
        List<String> list = new ArrayList<String>();
        list.add("Magci");
        // hasItem:Iterable变量中含有指定元素时,测试通过
        assertThat(list, hasItem("Magci"));

    }

    @Test
    public void testGetMap() {
        Map<String, String> map = new HashMap<String, String>();
        map.put("1key", "1value");
        map.put("2key", "2value");
        map.put("3key", "3value");
        map.put("4key", "4value");
        // hasEntry:Map变量中含有指定键值对时,测试通过
        assertThat(map, hasEntry("4key", "4value"));
        // hasKey:Map变量中含有指定键时,测试通过
        assertThat(map, hasKey(anyOf(equalTo("1key"), equalTo("2key"), equalTo("3key"), equalTo("4key"))));
        // hasValue:Map变量中含有指定值时,测试通过
        assertThat(map, hasValue(anyOf(equalTo("xvalue"), equalTo("1value"))));
    }

}

 

assertThat详解 ---来自 http://www.cnblogs.com/Firefly727/archive/2011/07/05/2098625.html

㈠ 一般匹配符

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" ) );

http://www.cnblogs.com/Dreama/articles/2211154.html

 

一般匹配符
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));

 http://langgufu.iteye.com/blog/1893927

 

 

 

 

 

assertThat

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));

http://0411.iteye.com/blog/1048303

posted @ 2015-12-01 22:24  沧海一滴  阅读(1880)  评论(0编辑  收藏  举报