本文主要是介绍dbunit study note,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.准备环境
dbunit的jar包,2.3.0
配置数据库:这里用mysql5.0
2.一些方法
获取所有数据库中内容
Class driverClass = Class.forName("com.mysql.jdbc.Driver");
Connection jdbcConnection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=GBK","root", "111111");
IDatabaseConnection databaseConnection = new DatabaseConnection(jdbcConnection);
IDataSet dataSet = databaseConnection.createDataSet();
FlatXmlDataSet.write(dataSet, new FileOutputStream("c:/test.xml"));
获取特定表的数据
Class driverClass = Class.forName("com.mysql.jdbc.Driver");Connection jdbcConnection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=GBK","root", "111111");
IDatabaseConnection databaseConnection = new DatabaseConnection(jdbcConnection);QueryDataSet queryDataSet = new QueryDataSet(databaseConnection);queryDataSet.addTable("Product", "select p.id from Product p");queryDataSet.addTable("demo");FlatXmlDataSet.write(queryDataSet, new FileOutputStream("c:/test.xml"));
获取特定表以及其他依赖本表的数据
Class driverClass = Class.forName("com.mysql.jdbc.Driver");Connection jdbcConnection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=GBK","root", "111111");
IDatabaseConnection databaseConnection = new DatabaseConnection(jdbcConnection);String[] depTableNames = TablesDependencyHelper.getAllDependentTables(databaseConnection, "Product");IDataSet depDataset = databaseConnection.createDataSet(depTableNames);FlatXmlDataSet.write(depDataset, new FileOutputStream("c:/demo.xml"));
生成DTD文件
Class driverClass = Class.forName("com.mysql.jdbc.Driver");Connection jdbcConnection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=GBK","root", "111111");
IDatabaseConnection databaseConnection = new DatabaseConnection(jdbcConnection);FlatDtdDataSet.write(databaseConnection.createDataSet(),new FileOutputStream("c:/test.dtd"));
和单元测试相关的:
1.准备测试数据文件
<?xml version='1.0' encoding='UTF-8'?>
<dataset><demo ID="1" name="one" unitPrice="12.0"/>
</dataset>
2.继承DBTestCase来实现测试代码,DBTestCase的底层是继承了Junit的TestCase。DBTestCase依赖IDatabaseTester。
public class SampleTest extends DBTestCase
{public SampleTest(String name){super( name );System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_DRIVER_CLASS, "org.hsqldb.jdbcDriver" );System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_CONNECTION_URL, "jdbc:hsqldb:sample" );System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_USERNAME, "sa" );System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_PASSWORD, "" );// System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_SCHEMA, "" );}protected IDataSet getDataSet() throws Exception{return new FlatXmlDataSet(new FileInputStream("dataset.xml"));}
}
3.默认情况下,是clean insert,如果要覆盖,则覆盖getSetUpOperation() 和 getTearDownOperation() 方法。
public class SampleTest extends DBTestCase
{...protected DatabaseOperation getSetUpOperation() throws Exception{return DatabaseOperation.REFRESH;}protected DatabaseOperation getTearDownOperation() throws Exception{return DatabaseOperation.NONE;}...
}
这篇关于dbunit study note的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!