使用JUnit測試一個應(yīng)用程序
現(xiàn)在已經(jīng)準(zhǔn)備好測試JN_test應(yīng)用程序。為了測試,還需要使用JUnit Wizard創(chuàng)建一個新的class來擴(kuò)展JUnit測試用例。要使用此wizard,請在Package Explorer 中的JN_test上單擊右鍵,并且選擇New->Other來打開一個New對話框,如圖所示:
現(xiàn)在展開Java結(jié)點并選擇JUnit,然后再選擇JUnit Test Case,單擊Next按鈕,如圖:
通常情況下JUnit類命名要和被它測試的類同名,并在其后面添加Test。所以命名為JN_testTest。另外選擇setUp和tearDown方法,這些方法建立和清理在測試用例中的數(shù)據(jù)和(或者)對象(它們在JUnit中的術(shù)語為fixtures)。按照上圖填好后,點擊Next。如圖:
在此對話框中,選擇你想要測試的方法,這樣JUnit Wizard能夠為它們創(chuàng)建存根(stub)。因為我們想要測試allocate,see和get,選擇它們,并點擊Finish按鈕來創(chuàng)建JN_testTest class。如下所示:在JN_testTest class中為allocate,set和get方法都創(chuàng)建了一個方法存根: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() {
}
}
下一步是在這些存根中添加代碼,以讓它們來調(diào)用JN_test類中的allocate,set和get方法,這樣能對結(jié)果使用JUnit斷言方法。我們將需要一個JN_test類的一個對象來調(diào)用這些方法,將其命名為testObject。要創(chuàng)建testObject使用JUnit代碼中的setUp方法。此方法在JUnit測試開始之前被調(diào)用,這樣我們將從將要測試的JN_test類中創(chuàng)建testObject。
JN_test testObject;
.
.
.
protected void setUp() throws Exception {
super.setUp();
testObject = new JN_test();
}
現(xiàn)在可以用這個對象來進(jìn)行測試了。比如,allocate方法是用來創(chuàng)建一個整型數(shù)組并返回此數(shù)組的,所以在testAllocate中使用assertNotNull測試并確信此數(shù)組不為空:
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方法從數(shù)組中取得數(shù)值,在testGet中用assertEquals來測試此方法。
public void testGet() {
assertEquals(testObject.get(1),1);
}
And the set method is supposed to return true if it@#s been suclearcase/" target="_blank" >ccessful, 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,它測試的是被創(chuàng)建的數(shù)組不能為null:
public void testAllocate( ) {
assertNotNull(testObject.allocate( ));
}
看看第一個trace:哦,這個地方遺漏了創(chuàng)建數(shù)組,我們修正代碼:
public int[] allocate()
{
array = new int[3];
array[0] = 0;
array[1] = 1;
array[2] = 2;
return array;
}
現(xiàn)在再運(yùn)行JN_testTest,現(xiàn)在只有testGet和testSet兩處錯誤,如圖:
testGet和testSet出了什么問題?這里的問題是所有的JUnit測試都是單獨運(yùn)行的。也是說盡管第一個測試testAllocate調(diào)用了allocate方法,但是它并沒有被接下來的兩個測試testGet和testSet調(diào)用。所以為了給這兩個測試初始化其數(shù)組,必須在testGet和testSet中都調(diào)用allocate方法。添加以下代碼:
public void testGet() {
testObject.allocate();
assertEquals(testObject.get(1),1);
}
public void testSet() {
testObject.allocate();
assertTrue(testObject.set(2,4));
}
現(xiàn)在運(yùn)行測試,全部都通過。JUnit視圖中頂部的菜單欄為綠色,如圖所示:
如上所示,JUnit提供了相當(dāng)簡單的方式來創(chuàng)建一組標(biāo)準(zhǔn)的測試,我們只需要動幾下鼠標(biāo)可以實現(xiàn)。只要測試被創(chuàng)建,你只需要運(yùn)行你創(chuàng)建的JUnit類。
然而,需要注意的是JUnit只是用一組測試來檢驗兼容性,如果你的代碼中存在問題,或者你不知道怎么辦,這時你需要進(jìn)行debug。(全文完)