【Java开发技术之程序测试】Junit4 新功能学习总结
1.Junit4 标注总结
2.测试方法总结
3.assertThat语句的基本使用
该语句是代替原来的断言语句,以一种可读性更强的形式呈现断言。
使用时,注意导入:(Eclipse某些版本不能自动导入)
import static org.junit.Assert.assertThat;      
import static org.hamcrest.CoreMatchers.*;
常用语句如下:
另外,其他常用语句如下:
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举例:
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. http://edgibbs.com/junit-4-with-hamcrest/2. www.vogella.de/articles/JUnit/article.html  
                     
                    
                 
                    
                


 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号