Solo  当前访客:0 开始使用

断言使用


一、依赖

<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest-library</artifactId>
    <version>1.3</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>

二、一般匹配符

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

六、代码使用



 		    /**数值匹配**/
35                  //测试变量是否大于指定值
36                  assertThat(50, greaterThan(50));
37                  //测试变量是否小于指定值
38                  assertThat(100, lessThan(100));
39                  //测试变量是否大于等于指定值
40                  assertThat(50, greaterThanOrEqualTo(50));
41                  //测试变量是否小于等于指定值
42                  assertThat(100, lessThanOrEqualTo(100));
44                  //测试所有条件必须成立
45                  assertThat(60, 	allOf(greaterThan(50),lessThan(100)));
46                  //测试只要有一个条件成立
47                  assertThat(60, anyOf(greaterThanOrEqualTo(50), lessThanOrEqualTo(100)));
50                  //测试变量值等于指定值
51                  assertThat(100, is(100));
52                  //测试变量不等于指定值
53                  assertThat(50, not(50));
54   
55                  /**字符串匹配**/
56                  String url = "http://www.nick.com";
57                  //测试变量是否包含指定字符
58                  assertThat(url, containsString("nick"));
59                  //测试变量是否已指定字符串开头
60                  assertThat(url, startsWith("http://"));
61                  //测试变量是否以指定字符串结尾
62                  assertThat(url, endsWith(".com"));
63                  //测试变量是否等于指定字符串
64                  assertThat(url, equalTo("http://www.nick.com"));
65                  //测试变量再忽略大小写的情况下是否等于指定字符串
66                  assertThat(url, equalToIgnoringCase("http://www.nick.com"));
67                  //测试变量再忽略头尾任意空格的情况下是否等于指定字符串
68                  assertThat(url, equalToIgnoringWhiteSpace("http://www.nick.com"));
69   
70   
71                  /**集合匹配**/
72   
73                  List<String> strList= new ArrayList<User>();
74                  strList.add("1");
75                  strList.add("2");
76   
77                  //测试集合中是否还有指定元素
78                  assertThat(strList, hasItem(key));
79                  assertThat(strList, hasItem(key));
80   
81                  /**Map匹配**/
82                  Map<String,String> strMap = new HashMap<String,String>();
83                  strMap .put("1", test1);
84                  strMap .put("2", test2);
85   
86                  //测试map中是否还有指定键值对
87                  assertThat(strMap , hasEntry("", test1));
88                  //测试map中是否还有指定键
89                  assertThat(strMap , hasKey(""));
90                  //测试map中是否还有指定值
91                  assertThat(strMap , hasValue(key));
0 0