歡迎您光臨本站 註冊首頁

關於junit實現過程詳細介紹

←手機掃碼閱讀     火星人 @ 2014-03-10 , reply:0

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

  第一步:

  去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,如果一切正確,就會打開應用程序.在下拉菜單中尋找程序自帶的例子,比如:junit.samples.AllTests,點擊「Run」觀察結果.

  第三步:

  實現自己的TEST計劃,目前有一個叫MyBean的資料庫操作類需要測試,如下:

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(){ //初始化資料庫
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 Success!";
}
catch(Exception e){
System.out.println(e);
return "Connection Error!";
}
}

public String gogo(String lmdm){ //查詢資料庫
try{
stmt=conn.createStatement();
String sql="select * from TB_LM where N_LMDM='" lmdm "'";
rs=stmt.executeQuery(sql); //執行查詢
while (rs.next()){
result=rs.getString("N_SJID");
}
}
catch(Exception e){
result=e.toString();
}
finally { //關閉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;
}
}

  接著,創建一個測試類:TestMyBean,如下:

package junit.samples;

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

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

private MyBean aName; //構造被測類的對象

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

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

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

public void testCon() { //對預期的值和con方法比較
Assert.assertTrue(!aName.equals(null)); //斷言
Assert.assertEquals("Connection Success!",aName.con());
}
public void testGogo() { //對預期的值和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()是一個很特殊的靜態方法,它會使用反射動態的創建一個包含所有的testXxxx方法的測試套件,確定有多少個測試可以執行;testCon()方法對MyBean的Con方法進行測試,並斷言(Assert)結果是"Connection Success!",並在Assert.assertEquals()方法中驗證;testGogo()方法和testCon()方法類似.

  第四步:

  把TestMyBean、MyBean類編譯成*.class文件,在Junit的控制台上選擇剛才定義的TestMyBean類,並運行.如果一切正確,就會顯示綠條,證明測試正確.如果顯示紅色,在Results中會有相應顯示,根據提示檢查MyBean類中的錯誤.一般的,只要斷言符合MyBean類的規範,TestMyBean類幾乎不可能出錯.

  一些擴展:

  對於WEB應用程序,我們可以把Junit引入,只需適當配置環境.另外,可以把眾多的測試類集成到一起,形成總測試類,並且只需要實現suite()方法,例如:

public static Test suite ( ) {
TestSuite suite= new TestSuite("All JUnit Tests");
suite.addTest(VectorTest.suite());
suite.addTest(TestMyBean.suite());
return suite;
}


[火星人 ] 關於junit實現過程詳細介紹已經有498次圍觀

http://coctec.com/docs/java/show-post-61771.html