代码 :
public class TestDbunit {
public static JdbcDatabaseTester tester;
IDataSet dataSet_Insert ;
IDataSet dataSet_init;
IDataSet dataSet_bak;
@BeforeClass
protected void setUp() throws Exception{
System.out.println("BeforeClass");
IDatabaseConnection conn=tester.getConnection();
String[] depTableNames=new String[]{"ROLES","USERS","USER_ROLES"};
try{
IDataSet utput=conn.createDataSet(depTableNames);
FileOutputStream fos=new FileOutputStream("user-roles-bak.xml");
//备份数据
FlatXmlDataSet.write(output, fos);
//初始化数据
//dataSet_init=new FlatXmlDataSet(new File("user-roles.xml"));
//DatabaseOperation.CLEAN_INSERT.execute(conn, dataSet_init);
}finally{
conn.close();
}
}
@BeforeTest
public void loadDB()throws Exception{
System.out.println("@BeforeTest");
tester=new JdbcDatabaseTester( "com.mysql.jdbc.Driver" ,
"jdbc:mysql://localhost:3306/test" , "root" , "" );
dataSet_init=new FlatXmlDataSet(new File("user-roles.xml"));
IDatabaseConnection conn=tester.getConnection();
try{
DatabaseOperation.CLEAN_INSERT.execute(conn, dataSet_init);
}finally{
conn.close();
}
}