您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > DBunit
DBUnit入門
作者:網(wǎng)絡轉載 發(fā)布時間:[ 2013/12/18 10:39:45 ] 推薦標簽:

那么在執(zhí)行該方法后,數(shù)據(jù)庫的student表里的數(shù)據(jù)是這樣的:

id name sex birthday
0001 翁仔 m 1979-12-31
0002 王翠花 f 1982-08-09
0088 王耳朵 m 1982-01-01

然后我們說明如何對這2個方法進行單元測試。

實例展開
1 把準備數(shù)據(jù)和預想數(shù)據(jù)轉換成xml文件
student_pre.xml

<?xml version='1.0' encoding="gb2312"?>
<dataset>
<student id="0001" name="翁仔" sex="m" birthday="1979-12-31"/>
<student id="0002" name="王翠花" sex="f" birthday="1982-08-09"/>
</dataset>


student_exp.xml

<?xml version='1.0' encoding="gb2312"?>
<dataset>
<student id="0001" name="翁仔" sex="m" birthday="1979-12-31"/>
<student id="0002" name="王翠花" sex="f" birthday="1982-08-09"/>
<student id="0088" name="王耳朵" sex="m" birthday="1982-01-01"/>
</dataset>

2 實裝setUp方法,詳細見代碼注釋。

protected void setUp() {
IDatabaseConnection connection =null;
try{
super.setUp();
//本例使用postgresql數(shù)據(jù)庫
Class.forName("org.postgresql.Driver");
//連接DB
Connection conn=DriverManager.getConnection("jdbc:postgresql:testdb.test","postgres","postgres");
//獲得DB連接
connection =new DatabaseConnection(conn);

//對數(shù)據(jù)庫中的操作對象表student進行備份
QueryDataSet backupDataSet = new QueryDataSet(connection);
backupDataSet.addTable("student");
file=File.createTempFile("student_back",".xml");//備份文件
FlatXmlDataSet.write(backupDataSet,new FileOutputStream(file));

//準備數(shù)據(jù)的讀入
IDataSet dataSet = new FlatXmlDataSet( new FileInputStream("student_pre.xml"));
DatabaseOperation.CLEAN_INSERT.execute(connection,dataSet);

}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(connection!=null) connection.close();
}catch(SQLException e){}
}
}

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