创建测试用例的测试脚本过程如下:
(1) //测试前,装载初始数据
dbAction.dbActionBeforeTest("testdata\Initial.xml");
(2) //断言测试实际数据和测试期望数据
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) // 测试后,设置数据库数据
dbAction.dbActionAfterTest();
4.其中Initial.xml内容为:
<?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内容为:
<?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是用到的表的名字,后面的是表中的字段名字。
创建xml文件时,可以把数据库表里的数据导出成该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);
//将整个TSYS_USER表里的数据导出到xml文件里
//dataSet.addTable("TSYS_USER");
//将TSYS_USER表里符合条件的数据导出到xml文件里
dataSet.addTable("TSYS_USER","select * FROM JRESTESTER.TSYS_USER WHERE USER_ID='00001'");
//导出到dbunit.xml文件里
FlatXmlDataSet.write(dataSet,new FileOutputStream("dbunit.xml"));
}