JUnit4單元測試、JUnit4使用詳解、assertThat用法

一.JUnit基本介紹

一、簡介       

         JUnit是一個開放源碼的、Java語言的單元測試框架,用於測試指望結果的斷言(Assertion);由 Erich Gamma 和 Kent Beck 編寫的一個迴歸測試框架(regression testing framework)。Junit測試是程序員測試,即所謂白盒測試,由於程序員知道被測試的軟件如何(How)完成功能和完成什麼樣(What)的功能。多數Java的開發環境都已經集成了JUnit做爲單元測試的工具,如Eclipse。程序員

        測試通常分爲:單元測試、集成測試(主要看一塊代碼加進去後,系統會不會有問題)、驗收測試和壓力測試。單元測試的最基本的一個功能是能進行自動化測試。單元測試都是經過斷言的方式來肯定結果是否正確,便是用Assert。編程

二、優勢

        junit是在極限編程重構(refactor)中被極力推薦使用的工具,由於在實現自動單元測試的狀況下能夠大大的提升開發的效率。框架

        極限編程:要求在編寫代碼以前先寫測試,這樣能夠強制你在寫代碼以前好好的思考代碼(方法)的功能和邏輯,不然編寫的代碼很不穩定,那麼你須要同時維護測試代碼和實際代碼,這個工做量就會大大增長。所以在極限編程中,基本過程是這樣的:構思-> 編寫測試代碼-> 編寫代碼-> 測試,並且編寫測試和編寫代碼都是增量式的,寫一點測一點,在編寫之後的代碼中若是發現問題能夠較快的追蹤到問題的緣由,減少迴歸錯誤的糾錯難度。less

        重構:其好處和極限編程中是相似的,由於重構也是要求改一點測一點,減小回歸錯誤形成的時間消耗。maven

三、Junit3與Junit4

        在junit3中,若是某個類是測試類,必須將其繼承類TestCase,若是某個方法是測試方法,必須讓這個方法以testXX開頭,若是但願指定某個測試方法運行以前運行某個初始化方法,這個方法的名稱必須是setUp,若是但願在某個測試方法運行以後運行某個釋放資源的方法,這個方法的名稱必須是tearDown。
        在junit4中,一個POJO類就是一個測試類,測試方法經過@Test來標識,初始化方法經過@Before來標識,釋放資源的方法經過@After來標識,可是爲了讓junit4的測試類在junit3中也可使用,習慣於把初始化方法命名爲setUp,釋放資源的方法命名爲tearDown。Test中的測試方法通常以Test來開始。其中標識爲Before註解的方法,每次運行測試類,都會執行標識爲@After與@Before的方法。ide

 

二.JUnit單元測試

        因爲JUnit4引用了不少Annotation註解之後,用JUnit進行單元測試比較方便,主要關鍵點在於:註解,斷言的使用;函數

        如下爲測試的示例:工具

待測試類單元測試

package com.test;

/**
 * 計算類
 * @author helen.
 * @Time 2016年5月13日
 * @Version 1.0
 */
public class Calculator {
	
	public int add(int a, int b) {  
		System.out.println("=======正在執行加法");
        return a + b;  
    }  
  
    public int minus(int a, int b) {  
    	System.out.println("=======正在執行減法");
        return a - b;  
    }  
  
    public int square(int n) {  
    	System.out.println("=======正在執行平方計算");
        return n * n;  
    }  
      
    //Bug : 死循環  
    public void squareRoot(int n) {  
    	System.out.println("=======正在執行死循環的方法");
        for(; ;)  
            ;  
    }  
      
    public int multiply(int a, int b) {  
    	System.out.println("=======正在執行乘法");
        return a * b;  
    }  
  
    public int divide(int a, int b) throws Exception { 
    	System.out.println("=======正在執行除法");
        if (0 == b) {  
            throw new Exception("除數不能爲零");  
        }  
        return a / b;  
    }
    
}

單元測試類測試

package com.test;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import static org.hamcrest.Matchers.*;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;

/**
 * 計算類單元測試
 * @author helen.
 * @Time 2016年5月13日
 * @Version 1.0
 */
public class CalculatorTest {

	private Calculator cal = new Calculator();  
	  
	/**
	 * 注意
	 * 必須爲靜態方法static...由於方法將在類被裝載的時候就被調用(那時候還沒建立實例) 
	 * 
	 * @author helen, 2016年5月13日.
	 */
    @BeforeClass 
    public static void before()  
    {  
        System.out.println("global");  
    }  
  
    @AfterClass  
    public static void after() {  
        System.out.println("global destroy");  
    }  
  
    @Before  
    public void setUp() throws Exception {  
        System.out.println("one test begin");  
    }  
  
    @After  
    public void tearDown() throws Exception {  
        System.out.println("one test end");  
    }  
  
    @Test  
    @Ignore  
    public void testAdd() {  
        int result = cal.add(1, 2);  
        Assert.assertEquals(30, result);  
    }  
  
    @Test  
    public void testMinus() {  
        int result = cal.minus(5, 2);  
        Assert.assertThat(result, greaterThan(2));
    }

    @Test  
    public void testMultiply() {  
        int result = cal.multiply(4, 2);  
        Assert.assertEquals(8, result);  
    }  
  
    @Test(timeout = 1000) // 單位爲毫秒  
    public void testSquareRoot() {  
        cal.squareRoot(4);  
    }  
  
    @Test(expected = Exception.class)  
    public void testDivide() throws Exception {  
        cal.divide(4, 0);  
    } 

}

    運行測試用:在Eclipse裏Run As -> JUnit Test,運行測試類,Eclipse的JUnit的View顯示以及控制檯的輸出以下截圖: 

        能夠看到,CalculatorTest類中總共有5個測試用例,ignore了一個,3個測試用例經過,testSquareRoot測試不經過(由於超時),因此整個的測試結果飄紅了。

 

三.JUnit使用詳解

一、註解說明

@Test:
代表該方法是一個測試方法
 
@BeforeClass 和 @AfterClass:
測試用例初始化時執行 @BeforeClass方法,當全部測試執行完畢以後,執行@AfterClass進行收尾工做。標註、@BeforeClass 和 @AfterClass的方法必須是static的,由於方法將在類被裝載的時候就被調用,那時候還沒建立測試對象實例。
 
@Before: 
使用了該元數據的方法在每一個測試方法執行以前都要執行一次。
@After: 
使用了該元數據的方法在每一個測試方法執行以後要執行一次。
 
@Test(expected=*.class) :
經過@Test元數據中的expected屬性驗證是否拋出指望的異常,expected屬性的值是一個異常的類型,若是拋出了指望的異常,則測試經過,不然不經過。
 
@Test(timeout=xxx):
該元數據傳入了一個時間(毫秒)給測試方法,若是測試方法在制定的時間以內沒有運行完,則測試也失敗。
 
@Ignore: 
該元數據標記的測試方法在測試中會被忽略。同時能夠爲該標籤傳遞一個String的參數,來代表爲何會忽略這個測試方法。好比:@lgnore("該方法尚未實現"),在執行的時候,僅會報告該方法沒有實現,而不會運行測試方法。

 

二、經常使用斷言

        在test方法內除了使用Assert的assertEquals()方法外,還能使用assertFalse()、assertTrue()、assertNull()、assertNotNull()、assertSame()、assertNotSame()等斷言函數。並且若是使用的是Junit4,結合Hamcrest,使用
assertThat([value], [matcher statement])方法能夠實現更靈活的斷言判斷(前提是引入hamcrest的jar包)。

        若是不須要用到assertThat,則只須要用Eclipse自帶的junit4工具便可;若是須要用到assertThat,則最好不要Eclipse自帶的工具,從新經過maven方式引入jar

<dependency>
		    <groupId>junit</groupId>
		    <artifactId>junit</artifactId>
		    <version>4.11</version>
		</dependency>				
		<dependency>
		    <groupId>org.hamcrest</groupId>
		    <artifactId>hamcrest-all</artifactId>
		    <version>1.3</version>
		</dependency>

 

四.assertThat用法(斷言使用)

Hamcrest框架和assertThat:

1.JUnit4.4引入了Hamcrest框架,Hamcest提供了一套匹配符Matcher,這些匹配符更接近天然語言,可讀性高,更加靈活;
2.使用全新的斷言語法:assertThat,結合Hamcest提供的匹配符,只用這一個方法,就能夠實現全部的測試;
3.assertThat語法以下:
     assertThat(T actual, Matcher<T> matcher);
     assertThat(String reason, T actual, Matcher<T> matcher);
     其中actual爲須要測試的變量,matcher爲使用Hamcrest的匹配符來表達變量actual指望值的聲明;
4.注意事項:
     a.必須導入JUnit4.4以後的版本才能使用assertThat方法;
     b.不須要繼承TestCase類,可是須要測試方法前必須加「@Test」。

 

經常使用:

// is匹配符代表若是前面待測的object等於後面給出的object,則測試經過 
assertThat( testedObj, is( object) ); 
 
// containsString匹配符代表若是測試的字符串包含指定的子字符串則測試經過
 assertThat( testedString, containsString( "developerWorks" ) );
 
// greaterThan匹配符代表若是所測試的數值testedNumber大於16.0則測試經過
 assertThat( testedNumber, greaterThan(16.0) ); 
 
// closeTo匹配符代表若是所測試的浮點型數testedDouble在20.0±0.5範圍以內則測試經過 
assertThat( testedDouble, closeTo( 20.0, 0.5 ) );
 
//hasItem匹配符代表被測的迭代對象含有元素element項則測試經過assertThat(iterableObject, hasItem (element));

 

通常匹配符:

一、assertThat( testedNumber, allOf( greaterThan(8), lessThan(16) ) );
註釋: allOf匹配符代表若是接下來的全部條件必須都成立測試才經過,至關於「與」(&&)
二、assertThat( testedNumber, anyOf( greaterThan(16), lessThan(8) ) );
註釋:anyOf匹配符代表若是接下來的全部條件只要有一個成立則測試經過,至關於「或」(||)
三、assertThat( testedNumber, anything() );
註釋:anything匹配符代表不管什麼條件,永遠爲true
四、assertThat( testedString, is( "developerWorks" ) );
註釋: is匹配符代表若是前面待測的object等於後面給出的object,則測試經過
五、assertThat( testedString, not( "developerWorks" ) );
註釋:not匹配符和is匹配符正好相反,代表若是前面待測的object不等於後面給出的object,則測試經過

 

字符串相關匹配符

一、assertThat( testedString, containsString( "developerWorks" ) );
註釋:containsString匹配符代表若是測試的字符串testedString包含子字符串"developerWorks"則測試經過
二、assertThat( testedString, endsWith( "developerWorks" ) ); 
註釋:endsWith匹配符代表若是測試的字符串testedString以子字符串"developerWorks"結尾則測試經過
三、assertThat( testedString, startsWith( "developerWorks" ) ); 
註釋:startsWith匹配符代表若是測試的字符串testedString以子字符串"developerWorks"開始則測試經過
四、assertThat( testedValue, equalTo( expectedValue ) ); 
註釋: equalTo匹配符代表若是測試的testedValue等於expectedValue則測試經過,equalTo能夠測試數值之間,字
符串之間和對象之間是否相等,至關於Object的equals方法
五、assertThat( testedString, equalToIgnoringCase( "developerWorks" ) ); 
註釋:equalToIgnoringCase匹配符代表若是測試的字符串testedString在忽略大小寫的狀況下等於"developerWorks"則測試經過
六、assertThat( testedString, equalToIgnoringWhiteSpace( "developerWorks" ) );
註釋:equalToIgnoringWhiteSpace匹配符代表若是測試的字符串testedString在忽略頭尾的任意個空格的狀況下等
於"developerWorks"則測試經過,注意:字符串中的空格不能被忽略

 

數值相關匹配符

一、assertThat( testedDouble, closeTo( 20.0, 0.5 ) );
註釋:closeTo匹配符代表若是所測試的浮點型數testedDouble在20.0±0.5範圍以內則測試經過
二、assertThat( testedNumber, greaterThan(16.0) );
註釋:greaterThan匹配符代表若是所測試的數值testedNumber大於16.0則測試經過
三、assertThat( testedNumber, lessThan (16.0) );
註釋:lessThan匹配符代表若是所測試的數值testedNumber小於16.0則測試經過
四、assertThat( testedNumber, greaterThanOrEqualTo (16.0) );
註釋: greaterThanOrEqualTo匹配符代表若是所測試的數值testedNumber大於等於16.0則測試經過
五、assertThat( testedNumber, lessThanOrEqualTo (16.0) );
註釋:lessThanOrEqualTo匹配符代表若是所測試的數值testedNumber小於等於16.0則測試經過

 

collection相關匹配符

一、assertThat( mapObject, hasEntry( "key", "value" ) ); 註釋:hasEntry匹配符代表若是測試的Map對象mapObject含有一個鍵值爲"key"對應元素值爲"value"的Entry項則測試經過 二、assertThat( iterableObject, hasItem ( "element" ) ); 註釋:hasItem匹配符代表若是測試的迭代對象iterableObject含有元素「element」項則測試經過 三、assertThat( mapObject, hasKey ( "key" ) ); 註釋: hasKey匹配符代表若是測試的Map對象mapObject含有鍵值「key」則測試經過 四、assertThat( mapObject, hasValue ( "key" ) ); 註釋:hasValue匹配符代表若是測試的Map對象mapObject含有元素值「value」則測試經過

相關文章
相關標籤/搜索