Junit4 新功能学习总结

    

1.Junit4 标注总结

image

2.测试方法总结

image

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.0, 0.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. import static org.hamcrest.CoreMatchers.*;
    6. import static org.junit.Assert.assertThat;
    7. public class HamcrestExamples {
    8. @Test
    9. public void allOfExampleShowsAllMatchersMustAllBeTrue() throws Exception {
    10. assertThat("Hello", is(allOf(notNullValue(), instanceOf(String.class), equalTo("Hello"))));
    11. }
    12. @Test
    13. public void allOfExampleShowsFailingIfOneMatcherDoesNotMatch() throws Exception {
    14. assertThat("Hello", is(not(allOf(notNullValue(), instanceOf(Integer.class)))));
    15. }
    16. @Test
    17. public void anyExampleChecksThatClassIsOfSameType() throws Exception {
    18. assertThat("Hello", is(any(String.class)));
    19. }
    20. @Test
    21. public void anyExampleShowsStringIsAlsoAnObject() throws Exception {
    22. assertThat("Hello", is(any(Object.class)));
    23. }
    24. @Test
    25. public void anyOfExampleReturnsTrueIfOneMatches() throws Exception {
    26. assertThat("Hello", is(anyOf(nullValue(), instanceOf(String.class), equalTo("Goodbye"))));
    27. }
    28. @Test
    29. public void anyOfExampleFailingIfAllMatchersAreFalse() throws Exception {
    30. assertThat("Hello", is(not(anyOf(nullValue(), instanceOf(Integer.class), equalTo("Goodbye")))));
    31. }
    32. @Test
    33. public void anythingExampleAlwaysReturnsTrue() throws Exception {
    34. assertThat("Hello", is(anything()));
    35. }
    36. // Feels very esoteric and not for typical usage used to override the description
    37. @Test
    38. public void describedAsExample() throws Exception {
    39. Matcher< ?> matcher = describedAs("My Description", anything());
    40. Description description = new StringDescription().appendDescriptionOf(matcher);
    41. assertThat("My Description", is(description.toString()));
    42. }
    43. @Test
    44. public void equalToExampleAddingTwoPlusTwo() throws Exception {
    45. assertThat(2 + 2, is(equalTo(4)));
    46. }
    47. @Test
    48. public void instanceOfExampleForString() throws Exception {
    49. assertThat("Hello", is(instanceOf(String.class)));
    50. }
    51. @Test
    52. public void isExampleShortCutForIsInstanceOfClass() throws Exception {
    53. assertThat("Hello", is(String.class));
    54. assertThat("Hello", instanceOf(String.class));
    55. }
    56. @Test
    57. public void isExampleShortCutAsJustSyntacticSugarUsedThreeTimes() throws Exception {
    58. assertThat("Hello", is(is(is(notNullValue()))));
    59. }
    60. @Test
    61. public void isExampleShortCutForIsEqualTo() throws Exception {
    62. assertThat("Hello", is("Hello"));
    63. assertThat("Hello", equalTo("Hello"));
    64. }
    65. @Test
    66. public void notExampleJustInvertsExpression() throws Exception {
    67. assertThat("Hello", is(not(instanceOf(Integer.class))));
    68. }
    69. @Test
    70. public void notNullValueExampleForString() throws Exception {
    71. assertThat("Hello", is(notNullValue()));
    72. }
    73. @Test
    74. public void notNullValueExampleForAClass() throws Exception {
    75. assertThat("Hello", is(notNullValue(Object.class)));
    76. }
    77. @Test
    78. public void nullValueExampleWithANull() throws Exception {
    79. assertThat(null, is(nullValue()));
    80. }
    81. @Test
    82. public void nullValueExampleWithANullType() throws Exception {
    83. Integer nothing = null;
    84. assertThat(nothing, is(nullValue(Integer.class)));
    85. }
    86. @Test
    87. public void sameInstanceExample() throws Exception {
    88. Object object = new Object();
    89. Object sameObject = object;
    90. assertThat(object, is(sameInstance(sameObject)));
    91. }
    92. }
    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 里面留空,为的是编译器通过编译。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值