您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > junit
junit實現(xiàn)過程
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時間:[ 2012/12/19 15:30:21 ] 推薦標簽:

測試分類:白箱測試、黑箱測試、單元測試、集成測試、功能測試...。白箱測試是指在知道被測試的軟件如何(How)完成功能和完成什么樣(What)的功能的條件下所作的測試,一般是由開發(fā)人員完成,單元測試是一種白箱測試,因為開發(fā)人員了解自己編寫的軟件。JUnit是由 Erich Gamma 和 Kent Beck 編寫的一個回歸測試框架,回歸測試是你不斷地對所編寫的代碼進行測試(如單元測試):編寫一些,測試一些,調(diào)試一些,然后循環(huán)這一過程,你會不斷地重復(fù)先前的測試,哪怕你正編寫其他的類。

第一步:
去Junit主頁(http://www.junit.org)下載新版本3.8.1程序包junit-3.8.1.zip。解開壓縮包到c:junit(可自定義)。

第二步:
假如目錄是c:junit那么,在classpath中加入:”c:junit;c:junitjunit.jar;“定義類路徑。在命令提示符下運行:java junit.swingui.TestRunner,如果一切正確,會打開應(yīng)用程序。在下拉菜單中尋找程序自帶的例子,比如:junit.samples.AllTests,點擊”Run“觀察結(jié)果。

第三步:
實現(xiàn)自己的TEST計劃,目前有一個叫MyBean的數(shù)據(jù)庫操作類需要測試,如下:
package junit.samples;

import java.sql.*;
import java.io.*;

public class MyBean{

Statement stmt=null;
ResultSet rs=null;
Connection conn=null;
String result=null;

public String con(){ //初始化數(shù)據(jù)庫
try{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url ="jdbc:mysql://192.168.0.88/weboa?user=root&password=";
conn= DriverManager.getConnection(url);
return "Connection Suclearcase/" target="_blank" >ccess!";
}
catch(Exception e){
System.out.println(e);
return "Connection Error!";
}
}

public String gogo(String lmdm){ //查詢數(shù)據(jù)庫

try{
stmt=conn.createStatement();
String sql="select * from TB_LM where N_LMDM='"+lmdm+"'";
rs=stmt.executeQuery(sql); //執(zhí)行查詢
while (rs.next()){
result=rs.getString("N_SJID");
}
}
catch(Exception e){
result=e.toString();
}
finally { //關(guān)閉JDBC資源
if(rs != null) try { rs.close(); } catch(SQLException ex) { ex.printStackTrace(); }
if(conn != null) try { conn.close(); } catch(SQLException ex) { ex.printStackTrace(); }
}
return result;
}
}
接著,創(chuàng)建一個測試類:TestMyBean,如下:
package junit.samples;

import junit.samples.MyBean;
import junit.framework.*;

public class TestMyBean extends TestCase { //TestCase的子類

private MyBean aName; //構(gòu)造被測類的對象

public TestMyBean(String name) {
super(name);
}

protected void setUp() { //進行初始化的任務(wù)
aName= new MyBean();
}

public static Test suite() { //進行測試
return new TestSuite(TestMyBean.class);
}

public void testCon() { //對預(yù)期的值和con方法比較
Assert.assertTrue(!aName.equals(null)); //斷言
Assert.assertEquals("Connection Success!",aName.con());
}
public void testGogo() { //對預(yù)期的值和gogo方法比較
aName.con();
Assert.assertTrue(!aName.equals(null)); //斷言
Assert.assertEquals("0",aName.gogo("1"));
}
}
解釋如下:
首先要引入待測試的類import junit.samples.MyBean;接著引入Junit框架import junit.framework.*;。與一個Servlet類似,需要繼承父類TestCase;在setUp()方法中實例化一個MyBean,供后面的測試方法使用;suite()是一個很特殊的靜態(tài)方法,它會使用反射動態(tài)的創(chuàng)建一個包含所有的testXxxx方法的測試套件,確定有多少個測試可以執(zhí)行;testCon()方法對MyBean的Con方法進行測試,并斷言(Assert)結(jié)果是"Connection Success!",并在Assert.assertEquals()方法中驗證;testGogo()方法和testCon()方法類似。

第四步:
把TestMyBean、MyBean類編譯成*.class文件,在Junit的控制臺上選擇剛才定義的TestMyBean類,并運行。如果一切正確,會顯示綠條,證明測試正確。如果顯示紅色,在Results中會有相應(yīng)顯示,根據(jù)提示檢查MyBean類中的錯誤。一般的,只要斷言符合MyBean類的規(guī)范,TestMyBean類幾乎不可能出錯。

一些擴展:
對于WEB應(yīng)用程序,我們可以把Junit引入,只需適當配置環(huán)境。另外,可以把眾多的測試類集成到一起,形成總測試類,并且只需要實現(xiàn)suite()方法,例如:
public static Test suite ( ) {
TestSuite suite= new TestSuite("All JUnit Tests");
suite.addTest(VectorTest.suite());
suite.addTest(TestMyBean.suite());
return suite;
}

軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd