使用dbunit用到的jar包有:
1.添加dbunit-2.3.0.jar
2.添加slf4j-api-1.5.3.jar
3.添加slf4j-jcl-1.5.3.jar
4.添加commons-logging.jar
另外如果没有对应数据库的驱动还需要添加对应数据库驱动jar
准备一个数据库表person
id username password
1 zhangsan 123
2 lisi 456
3 wangwu 789
实例一:导出数据库中的数据到xml文件
package com.test.dbunit; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class UsersDB { public void save(Users users){ Connection conn=null; try{ Class.forName("com.mysql.jdbc.Driver"); conn=DriverManager.getConnection("jdbc:mysql://localhost/test", "root", ""); String sql="insert into person(id,username,password) values(?,?,?)"; PreparedStatement ps =conn.prepareStatement(sql); ps.setInt(1, users.getId()); ps.setString(2,users.getUsername()); ps.setString(3,users.getPassword()); ps.executeUpdate(); }catch(Exception ex){ ex.printStackTrace(); }finally { if(null!= conn) { try { conn.close(); }catch(SQLException e){ e.printStackTrace(); } } } } } |
dbunit测试类:
package com.test.dbunit; import org.dbunit.DBTestCase; import org.dbunit.database.DatabaseConnection; import org.dbunit.database.IDatabaseConnection; import org.dbunit.dataset.IDataSet; import java.io.File; import java.io.FileInputStream; import java.sql.Connection; import java.sql.DriverManager; import org.dbunit.Assertion; import org.dbunit.DBTestCase; import org.dbunit.DatabaseTestCase; import org.dbunit.PropertiesBasedJdbcDatabaseTester; import org.dbunit.dataset.IDataSet; import org.dbunit.dataset.ITable; import org.dbunit.dataset.SortedTable; import org.dbunit.dataset.filter.DefaultColumnFilter; import org.dbunit.dataset.xml.FlatXmlDataSet; import org.dbunit.operation.DatabaseOperation; public class Test extends DatabaseTestCase { public Test() { // super(); System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_DRIVER_CLASS, "com.mysql.jdbc.Driver"); System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_CONNECTION_URL, "jdbc:mysql://localhost/test"); System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_USERNAME, "root"); System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_PASSWORD, ""); } /** * 给定数据集 */ @Override protected IDataSet getDataSet() throws Exception { System.out.println("init..."); return new FlatXmlDataSet(new FileInputStream("dbunit.xml")); } public void test1() throws Exception{ IDataSet dbDataSet = getConnection().createDataSet(); ITable dbTable = dbDataSet.getTable("person"); IDataSet xmlDataSet = new FlatXmlDataSet(new FileInputStream("dbunit.xml")); ITable xmlTable = xmlDataSet.getTable("person"); Assertion.assertEquals(xmlTable, dbTable); } /** * 对Users表插入一条记录做测试 * @throws Exception */ public void test5() throws Exception{ UsersDB db = new UsersDB(); Users users = new Users(); users.setId(9); users.setUsername("hello"); users.setPassword("world"); db.save(users); IDataSet dataSet = getConnection().createDataSet(); ITable actualTable = dataSet.getTable("person"); IDataSet dataSet2 = new FlatXmlDataSet(new File("expect.xml")); ITable expectedTable = dataSet2.getTable("person"); Assertion.assertEquals(expectedTable, actualTable); } /** * 在把xml里的数据导入到db里之前,需要对数据库里的表的数据做的操作。 */ protected DatabaseOperation getSetUpOperation() throws Exception { System.out.println("setup..."); return DatabaseOperation.CLEAN_INSERT; } /** * 测试执行完以后,需要对数据库里的表里的数据做的操作 */ protected DatabaseOperation getTearDownOperation() throws Exception { System.out.println("teardown..."); return DatabaseOperation.NONE; //清空数据库 // return DatabaseOperation.DELETE_ALL; } @Override protected IDatabaseConnection getConnection() throws Exception { // TODO Auto-generated method stub Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", ""); IDatabaseConnection connection = new DatabaseConnection(conn); return connection; } } |
package com.test.dbunit; import java.io.FileOutputStream; import java.sql.Connection; import java.sql.DriverManager; import org.dbunit.database.DatabaseConnection; import org.dbunit.database.IDatabaseConnection; import org.dbunit.database.QueryDataSet; import org.dbunit.dataset.xml.FlatXmlDataSet; /* * 单个表格导出 */ public class TestApp { public static void main(String[] args) throws Exception{ Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost/test", "root", ""); IDatabaseConnection connection = new DatabaseConnection(conn); QueryDataSet dataSet = new QueryDataSet(connection); //将整个person表里的数据导出到 xml文件里 dataSet.addTable("person"); //将users表里符合条件的数据导出到xml文件里 //dataSet.addTable("users","select * from users where id < 4"); //导出到dbunit.xml文件里 FlatXmlDataSet.write(dataSet,new FileOutputStream("dbunit.xml")); } } |
几点说明:jdbc:mysql://localhost/test中test为对应的数据库名称,dbunit.xml为对应的数据输出文件,导出的文件格式如下
实例三:表操作测试实例
此实例用到的几个类文件内容分别如下:
package com.test.dbunit; public class Users { private int id; private String username; private String password; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String userName) { this.username = userName; } public String getPassword() { return password; } public void setPassword(String passWord) { this.password = passWord; } } |
<?xml version='1.0' encoding='UTF-8'?> <dataset> <person id="1" username="zhangsan" password="123"/> <person id="2" username="lisi" password="456"/> <person id="3" username="wangwu" password="789"/> </dataset> |
实例二:验证数据库表内容与导出xml文件数据是否一致
package com.test.dbunit; import java.io.File; import java.io.FileInputStream; import org.dbunit.Assertion; import org.dbunit.DBTestCase; import org.dbunit.PropertiesBasedJdbcDatabaseTester; import org.dbunit.dataset.IDataSet; import org.dbunit.dataset.ITable; import org.dbunit.dataset.SortedTable; import org.dbunit.dataset.filter.DefaultColumnFilter; import org.dbunit.operation.DatabaseOperation; import junit.framework.TestCase; import org.dbunit.dataset.xml.FlatXmlDataSet; /* * 测试表数据是否一致 */ public class SimpleTest extends DBTestCase { public SimpleTest() { // super(); System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_DRIVER_CLASS,"com.mysql.jdbc.Driver"); System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_CONNECTION_URL,"jdbc:mysql://localhost/test"); System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_USERNAME,"root"); System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_PASSWORD,""); } /** * 给定数据集 */ @Override protected IDataSet getDataSet() throws Exception { System.out.println("init..."); return new FlatXmlDataSet(new FileInputStream("dbunit.xml")); } public void test1() throws Exception{ IDataSet dbDataSet = getConnection().createDataSet(); ITable dbTable = dbDataSet.getTable("person"); IDataSet xmlDataSet = new FlatXmlDataSet(new FileInputStream("dbunit.xml")); ITable xmlTable = xmlDataSet.getTable("person"); Assertion.assertEquals(xmlTable, dbTable); } } |