junit實現過程

來源:互聯網
上載者:User

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

第一步:

去Junit首頁(http://www.junit.org)下載最新版本程式包。解開壓縮包到c:\junit(可自訂)。

第二步:

假如目錄是c:\junit那麼,在classpath中加入:”c:\junit\;c:\junit\junit.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;
}
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.