創(chuàng)建測(cè)試用例的測(cè)試腳本過程如下:
(1) //測(cè)試前,裝載初始數(shù)據(jù)
dbAction.dbActionBeforeTest("testdata\Initial.xml");
(2) //斷言測(cè)試實(shí)際數(shù)據(jù)和測(cè)試期望數(shù)據(jù)
String actualTableName = "TSYS_USER";
String getActualTableSQL = "SELECT USER_ID,USER_NAME,USER_PWD,USER_TYPE,USER_STATUS,
LOCK_STATUS,CREATE_DATE,MODIFY_DATE,PASS_MODIFY_DATE,REMARK,ORG_ID FROM JRESTESTER.TSYS_USER WHERE USER_ID='00001'";
String XMLFilePath = dbAction.setFilePath("testdata\Expected.xml");
String expectedTableName = "TSYS_USER";
dbAction.AssertionData(actualTableName, getActualTableSQL, XMLFilePath, expectedTableName);
(3) // 測(cè)試后,設(shè)置數(shù)據(jù)庫(kù)數(shù)據(jù)
dbAction.dbActionAfterTest();
4.其中Initial.xml內(nèi)容為:
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<TSYS_USER USER_ID="00001" USER_NAME="test00001" USER_PWD="7e5096f694565d5549f03a360cdb2a84" USER_TYPE="0" USER_STATUS="0" LOCK_STATUS="1" CREATE_DATE="20110926" MODIFY_DATE="20111008" PASS_MODIFY_DATE="20110928" REMARK="sdf" ORG_ID="0_000000"/>
</dataset>
Expected.xml內(nèi)容為:
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<TSYS_USER USER_ID="00001" USER_NAME="user2" USER_PWD="7e5096f694565d5549f03a360cdb2a84" USER_TYPE="0" USER_STATUS="0" LOCK_STATUS="1" CREATE_DATE="20110926" MODIFY_DATE="20111008" PASS_MODIFY_DATE="20110928" REMARK="sdf" ORG_ID="0_000000"/>
</dataset>
其中TSYS_USER是用到的表的名字,后面的是表中的字段名字。
創(chuàng)建xml文件時(shí),可以把數(shù)據(jù)庫(kù)表里的數(shù)據(jù)導(dǎo)出成該xml:
public static void main(String[] args) throws Exception{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection jdbcConnection = DriverManager.getConnection("jdbcracle:thin192.168.71.52:1521:JRESSTUDIO",
"jrestester", "jrestester");
IDatabaseConnection connection = new DatabaseConnection(jdbcConnection,"JRESTESTER");
QueryDataSet dataSet = new QueryDataSet(connection);
//將整個(gè)TSYS_USER表里的數(shù)據(jù)導(dǎo)出到xml文件里
//dataSet.addTable("TSYS_USER");
//將TSYS_USER表里符合條件的數(shù)據(jù)導(dǎo)出到xml文件里
dataSet.addTable("TSYS_USER","select * FROM JRESTESTER.TSYS_USER WHERE USER_ID='00001'");
//導(dǎo)出到dbunit.xml文件里
FlatXmlDataSet.write(dataSet,new FileOutputStream("dbunit.xml"));
}