博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Junit4 标注总结
阅读量:4032 次
发布时间:2019-05-24

本文共 9944 字,大约阅读时间需要 33 分钟。

1.Junit4 标注总结

2.测试方法总结

3.assertThat语句的基本使用

该语句是代替原来的断言语句,以一种可读性更强的形式呈现断言。

使用时,注意导入:(Eclipse某些版本不能自动导入)

import static org.junit.Assert.assertThat;

import static org.hamcrest.CoreMatchers.*;

常用语句如下:

  • allOf语句表示所有在括号内的测试都必须通过,该断言才成立。
  • any语句表示结果是某一个类及其子类的类型。
  • anyOf语句表示只要在括号内的任意一个语句成立,该断言则通过。
  • equalTo语句表示两者在数值上“相等”。
  • is语句在大多数时候表示一种判断的动作,例如上述几个例子。但在传入一个"XXX.class"这样的参数时,也可以代替instanceOf;另外,还可以替代equalTo。
  • not语句是“取反”。
  • notNullValue语句是判断不是null,与nullValue语句相反。
  • sameInstance语句表示"==",即是否指向同一个对象实例。

    另外,其他常用语句如下:

    1. assertThat(testedString, equalToIgnoringCase(expectedString));    
    2. /**equalToIgnoringWhiteSpace匹配符断言被测的字符串testedString   
    3. *在忽略头尾的任意个空格的情况下等于expectedString,   
    4. *注意:字符串中的空格不能被忽略   
    5. */    
    6. assertThat(testedString, equalToIgnoringWhiteSpace(expectedString);    
    7. /**containsString匹配符断言被测的字符串testedString包含子字符串subString**/    
    8. assertThat(testedString, containsString(subString) );    
    9. /**endsWith匹配符断言被测的字符串testedString以子字符串suffix结尾*/    
    10. assertThat(testedString, endsWith(suffix));    
    11. /**startsWith匹配符断言被测的字符串testedString以子字符串prefix开始*/    
    12. assertThat(testedString, startsWith(prefix));    
    13. /**closeTo匹配符断言被测的浮点型数testedDouble在20.0¡À0.5范围之内*/    
    14. assertThat(testedDouble, closeTo( 20.00.5 ));    
    15. /**greaterThan匹配符断言被测的数值testedNumber大于16.0*/    
    16. assertThat(testedNumber, greaterThan(16.0));    
    17. /** lessThan匹配符断言被测的数值testedNumber小于16.0*/    
    18. assertThat(testedNumber, lessThan (16.0));    
    19. /** greaterThanOrEqualTo匹配符断言被测的数值testedNumber大于等于16.0*/    
    20. assertThat(testedNumber, greaterThanOrEqualTo (16.0));    
    21. /** lessThanOrEqualTo匹配符断言被测的testedNumber小于等于16.0*/    
    22. assertThat(testedNumber, lessThanOrEqualTo (16.0));    
    23. /**hasEntry匹配符断言被测的Map对象mapObject含有一个键值为"key"对应元素值为"value"的Entry项*/    
    24. assertThat(mapObject, hasEntry("key""value" ) );    
    25. /**hasItem匹配符表明被测的迭代对象iterableObject含有元素element项则测试通过*/    
    26. assertThat(iterableObject, hasItem (element));    
    27. /** hasKey匹配符断言被测的Map对象mapObject含有键值“key”*/    
    28. assertThat(mapObject, hasKey ("key"));    
    29. /** hasValue匹配符断言被测的Map对象mapObject含有元素值value*/    
    30. assertThat(mapObject, hasValue(value));  
    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)); /**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));

    assertThat举例:

    1. import org.hamcrest.Description;   
    2. import org.hamcrest.Matcher;   
    3. import org.hamcrest.StringDescription;   
    4. import org.junit.Test;   
    5.   
    6. import static org.hamcrest.CoreMatchers.*;   
    7. import static org.junit.Assert.assertThat;   
    8.   
    9. public class HamcrestExamples {   
    10.   
    11.   @Test  
    12.   public void allOfExampleShowsAllMatchersMustAllBeTrue() throws Exception {   
    13.     assertThat("Hello", is(allOf(notNullValue(), instanceOf(String.class), equalTo("Hello"))));   
    14.   }   
    15.   
    16.   @Test  
    17.   public void allOfExampleShowsFailingIfOneMatcherDoesNotMatch() throws Exception {   
    18.     assertThat("Hello", is(not(allOf(notNullValue(), instanceOf(Integer.class)))));   
    19.   }   
    20.   
    21.   @Test  
    22.   public void anyExampleChecksThatClassIsOfSameType() throws Exception {   
    23.     assertThat("Hello", is(any(String.class)));   
    24.   }   
    25.   
    26.   @Test  
    27.   public void anyExampleShowsStringIsAlsoAnObject() throws Exception {   
    28.     assertThat("Hello", is(any(Object.class)));   
    29.   }   
    30.   
    31.   @Test  
    32.   public void anyOfExampleReturnsTrueIfOneMatches() throws Exception {   
    33.     assertThat("Hello", is(anyOf(nullValue(), instanceOf(String.class), equalTo("Goodbye"))));   
    34.   }   
    35.   
    36.   @Test  
    37.   public void anyOfExampleFailingIfAllMatchersAreFalse() throws Exception {   
    38.     assertThat("Hello", is(not(anyOf(nullValue(), instanceOf(Integer.class), equalTo("Goodbye")))));   
    39.   }   
    40.   
    41.   @Test  
    42.   public void anythingExampleAlwaysReturnsTrue() throws Exception {   
    43.     assertThat("Hello", is(anything()));   
    44.   }   
    45.   
    46.   // Feels very esoteric and not for typical usage used to override the description   
    47.   @Test  
    48.   public void describedAsExample() throws Exception {   
    49.     Matcher< ?> matcher = describedAs("My Description", anything());   
    50.     Description description = new StringDescription().appendDescriptionOf(matcher);   
    51.     assertThat("My Description", is(description.toString()));   
    52.   }   
    53.   
    54.   @Test  
    55.   public void equalToExampleAddingTwoPlusTwo() throws Exception {   
    56.     assertThat(2 + 2, is(equalTo(4)));   
    57.   }   
    58.   
    59.   @Test  
    60.   public void instanceOfExampleForString() throws Exception {   
    61.     assertThat("Hello", is(instanceOf(String.class)));   
    62.   }   
    63.   
    64.   @Test  
    65.   public void isExampleShortCutForIsInstanceOfClass() throws Exception {   
    66.     assertThat("Hello", is(String.class));   
    67.     assertThat("Hello", instanceOf(String.class));   
    68.   }   
    69.   
    70.   @Test  
    71.   public void isExampleShortCutAsJustSyntacticSugarUsedThreeTimes() throws Exception {   
    72.     assertThat("Hello", is(is(is(notNullValue()))));   
    73.   }   
    74.   
    75.   @Test  
    76.   public void isExampleShortCutForIsEqualTo() throws Exception {   
    77.     assertThat("Hello", is("Hello"));   
    78.     assertThat("Hello", equalTo("Hello"));   
    79.   }   
    80.   
    81.   @Test  
    82.   public void notExampleJustInvertsExpression() throws Exception {   
    83.     assertThat("Hello", is(not(instanceOf(Integer.class))));   
    84.   }   
    85.   
    86.   @Test  
    87.   public void notNullValueExampleForString() throws Exception {   
    88.     assertThat("Hello", is(notNullValue()));   
    89.   }   
    90.   
    91.   @Test  
    92.   public void notNullValueExampleForAClass() throws Exception {   
    93.     assertThat("Hello", is(notNullValue(Object.class)));   
    94.   }   
    95.   
    96.   @Test  
    97.   public void nullValueExampleWithANull() throws Exception {   
    98.     assertThat(null, is(nullValue()));   
    99.   }   
    100.   
    101.   @Test  
    102.   public void nullValueExampleWithANullType() throws Exception {   
    103.     Integer nothing = null;   
    104.     assertThat(nothing, is(nullValue(Integer.class)));   
    105.   }   
    106.   
    107.   @Test  
    108.   public void sameInstanceExample() throws Exception {   
    109.     Object object = new Object();   
    110.     Object sameObject = object;   
    111.     assertThat(object, is(sameInstance(sameObject)));   
    112.   }   
    113. }  
    import org.hamcrest.Description;import org.hamcrest.Matcher;import org.hamcrest.StringDescription;import org.junit.Test;import static org.hamcrest.CoreMatchers.*;import static org.junit.Assert.assertThat;public class HamcrestExamples {  @Test  public void allOfExampleShowsAllMatchersMustAllBeTrue() throws Exception {    assertThat("Hello", is(allOf(notNullValue(), instanceOf(String.class), equalTo("Hello"))));  }  @Test  public void allOfExampleShowsFailingIfOneMatcherDoesNotMatch() throws Exception {    assertThat("Hello", is(not(allOf(notNullValue(), instanceOf(Integer.class)))));  }  @Test  public void anyExampleChecksThatClassIsOfSameType() throws Exception {    assertThat("Hello", is(any(String.class)));  }  @Test  public void anyExampleShowsStringIsAlsoAnObject() throws Exception {    assertThat("Hello", is(any(Object.class)));  }  @Test  public void anyOfExampleReturnsTrueIfOneMatches() throws Exception {    assertThat("Hello", is(anyOf(nullValue(), instanceOf(String.class), equalTo("Goodbye"))));  }  @Test  public void anyOfExampleFailingIfAllMatchersAreFalse() throws Exception {    assertThat("Hello", is(not(anyOf(nullValue(), instanceOf(Integer.class), equalTo("Goodbye")))));  }  @Test  public void anythingExampleAlwaysReturnsTrue() throws Exception {    assertThat("Hello", is(anything()));  }  // Feels very esoteric and not for typical usage used to override the description  @Test  public void describedAsExample() throws Exception {    Matcher< ?> matcher = describedAs("My Description", anything());    Description description = new StringDescription().appendDescriptionOf(matcher);    assertThat("My Description", is(description.toString()));  }  @Test  public void equalToExampleAddingTwoPlusTwo() throws Exception {    assertThat(2 + 2, is(equalTo(4)));  }  @Test  public void instanceOfExampleForString() throws Exception {    assertThat("Hello", is(instanceOf(String.class)));  }  @Test  public void isExampleShortCutForIsInstanceOfClass() throws Exception {    assertThat("Hello", is(String.class));    assertThat("Hello", instanceOf(String.class));  }  @Test  public void isExampleShortCutAsJustSyntacticSugarUsedThreeTimes() throws Exception {    assertThat("Hello", is(is(is(notNullValue()))));  }  @Test  public void isExampleShortCutForIsEqualTo() throws Exception {    assertThat("Hello", is("Hello"));    assertThat("Hello", equalTo("Hello"));  }  @Test  public void notExampleJustInvertsExpression() throws Exception {    assertThat("Hello", is(not(instanceOf(Integer.class))));  }  @Test  public void notNullValueExampleForString() throws Exception {    assertThat("Hello", is(notNullValue()));  }  @Test  public void notNullValueExampleForAClass() throws Exception {    assertThat("Hello", is(notNullValue(Object.class)));  }  @Test  public void nullValueExampleWithANull() throws Exception {    assertThat(null, is(nullValue()));  }  @Test  public void nullValueExampleWithANullType() throws Exception {    Integer nothing = null;    assertThat(nothing, is(nullValue(Integer.class)));  }  @Test  public void sameInstanceExample() throws Exception {    Object object = new Object();    Object sameObject = object;    assertThat(object, is(sameInstance(sameObject)));  }}
    4. Test Suite的使用     
    在@Suite.SuiteClasses()中加入需要进行测试的类,例如Dice4Test.class。public class AllTests 里面留空,为的是编译器通过编译。
    另外,在Eclipse中统计测试代码行覆盖率和分支覆盖率时,请使用EclEmma插件。
    参考文献:
    1. 
    2.  

转载地址:http://pcebi.baihongyu.com/

你可能感兴趣的文章
用防火墙自动拦截攻击IP
查看>>
补充自动屏蔽攻击ip
查看>>
谷歌走了
查看>>
多线程使用随机函数需要注意的一点
查看>>
getpeername,getsockname
查看>>
让我做你的下一行Code
查看>>
浅析:setsockopt()改善程序的健壮性
查看>>
关于对象赋值及返回临时对象过程中的构造与析构
查看>>
VS 2005 CRT函数的安全性增强版本
查看>>
SQL 多表联合查询
查看>>
Visual Studio 2010:C++0x新特性
查看>>
drwtsn32.exe和adplus.vbs进行dump文件抓取
查看>>
cppcheck c++静态代码检查
查看>>
CLOSE_WAIT和TIME_WAIT
查看>>
在C++中使用Lua
查看>>
在Dll中调用自身的位图资源
查看>>
IP校验和详解
查看>>
C++中使用Mongo执行count和distinct运算
查看>>
一些socket的编程经验
查看>>
socket编程中select的使用
查看>>