第一步:
去Junit主頁(http://www.junit.org)下載最新版本3.8.1程序包junit-3.8.1.zip。解開壓縮包到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(); }
}
文章來源于領測軟件測試網 http://www.kjueaiud.com/