Eclipse學習4-在Eclipse中使用JUnit進行單元測試(下)

使用JUnit測試一個應用程序

現在已經準備好測試JN_test應用程序。爲了測試,還需要使用JUnit Wizard創建一個新的class來擴展JUnit測試用例。要使用此wizard,請在Package Explorer 中的JN_test上單擊右鍵,並且選擇New->Other來打開一個New對話框,如圖所示:

現在展開Java結點並選擇JUnit,然後再選擇JUnit Test Case,單擊Next按鈕,如圖:

通常情況下JUnit類命名要和被它測試的類同名,並在其後面添加Test。所以命名爲JN_testTest。另外選擇setUptearDown方法,這些方法建立和清理在測試用例中的數據和(或者)對象(它們在JUnit中的術語爲fixtures)。按照上圖填好後,點擊Next。如圖:

在此對話框中,選擇你想要測試的方法,這樣JUnit Wizard能夠爲它們創建存根(stub)。因爲我們想要測試allocate,seeget,選擇它們,並點擊Finish按鈕來創建JN_testTest class。如下所示:在JN_testTest class中爲allocatesetget方法都創建了一個方法存根:testAllocate, testSet, testGet

package net.csdn.blog;

import junit.framework.TestCase;

 

 

 

 

 

 

 

 

 

 

public class JN_testTest extends TestCase {

    /*

     * @see TestCase#setUp()

     */

    protected void setUp() throws Exception {

        super.setUp();

    }

    /*

     * @see TestCase#tearDown()

     */

    protected void tearDown() throws Exception {

        super.tearDown();

    }

    public void testAllocate() {

    }

    public void testGet() {

    }

    public void testSet() {

    }

}

下一步就是在這些存根中添加代碼,以讓它們來調用JN_test類中的allocatesetget方法,這樣就能對結果使用JUnit斷言方法。我們將需要一個JN_test類的一個對象來調用這些方法,將其命名爲testObject。要創建testObject使用JUnit代碼中的setUp方法。此方法在JUnit測試開始之前就被調用,這樣我們將從將要測試的JN_test類中創建testObject

JN_test testObject;

   .

   .

   .

    protected void setUp() throws Exception {

        super.setUp();

        testObject = new JN_test();

}

現在就可以用這個對象來進行測試了。比如,allocate方法是用來創建一個整型數組並返回此數組的,所以在testAllocate中使用assertNotNull測試並確信此數組不爲空:

public void testAllocate() {

        assertNotNull(testObject.allocate());

}

The get method is supposed to retrieve a value from the array, so we can test that method using assertEquals with a test value in testGet:

get方法從數組中取得數值,在testGet中用assertEquals來測試此方法。

public void testGet() {

        assertEquals(testObject.get(1),1);

    }

And the set method is supposed to return true if it's been successful, so we can test it with assertTrue like this

set方法在成功的時候返回true,使用assertTrue來測試它。

public void testSet() {

        assertTrue(testObject.set(2,4));

    }

在添加代碼後,選擇Package Explorer 中的JN_testTest,並選擇Run As->JUnit Test 菜單項,如圖所示:

上面的圖示說明這裏有錯誤,在JUnit視圖中,三個測試都被打上了叉,表示測試失敗,我們先檢查第一個測試,testAllocate,它測試的是被創建的數組不能爲null

 

 

看看第一個trace:哦,這個地方遺漏了創建數組,我們修正代碼:

public int[] allocate()

    {

        array = new int[3];

        array[0] = 0;

        array[1] = 1;

        array[2] = 2;

        return array;

}

現在再運行JN_testTest,現在只有testGettestSet兩處錯誤,如圖:

testGettestSet出了什麼問題?這裏的問題是所有的JUnit測試都是單獨運行的。也就是說盡管第一個測試testAllocate調用了allocate方法,但是它並沒有被接下來的兩個測試testGettestSet調用。所以爲了給這兩個測試初始化其數組,必須在testGettestSet中都調用allocate方法。添加以下代碼:

 

 

 

 

 

 

 

 

 

 

public void testGet() {

        testObject.allocate();

        assertEquals(testObject.get(1),1);

    }

 

 

 

 

 

 

 

 

 

 

    public void testSet() {

        testObject.allocate();

        assertTrue(testObject.set(2,4));

    }

現在運行測試,全部都通過。JUnit視圖中頂部的菜單欄爲綠色,如圖所示:

如上所示,JUnit提供了相當簡單的方式來創建一組標準的測試,我們只需要動幾下鼠標就可以實現。只要測試被創建,你只需要運行你創建的JUnit類。

然而,需要注意的是JUnit只是用一組測試來檢驗兼容性,如果你的代碼中存在問題,或者你不知道怎麼辦,這時你需要進行debug。(全文完)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章