【Java開發技術之程式測試】Junit4 新功能學習總結

來源:互聯網
上載者:User

1.Junit4 標註總結

2.測試方法總結

3.assertThat語句的基本使用

該語句是代替原來的Assert 陳述式,以一種可讀性更強的形式呈現斷言。

使用時,注意匯入:(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語句表示"==",即是否指向同一個對象執行個體。

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

    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 

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.