前言:本篇博客是翻译自http://dbunit.sourceforge.net/howto.html,本人是一名初学者,而且并非专业人员,完全出于自我学习的兴趣,以及交流的目的,有任何疑问或者发现错误请及时提醒,万分感谢。
预备知识:简单的JDBC操作,java基本语法数据库的简单操作。
RDBMS即关系数据库管理系统(Relational Database Management System)
IDENTITY属性及自动增长,示例identity(1,1),表示这一列将自动从1开始编号,每插入一行,这一列就增1,并且插入数据时不能手动为这列插入,这列的值是系统自动插入的。
一、Database setup with a DBTestCase subclass
1.Create your dataset file(创建数据库的数据文件)
Your test need some data to work with. This means you must create a dataset. In most situations you will work with xml datasets. You can manually create a flat XML dataset from scratch or create one by exporting some data from your database.
我们进行数据库测试,测试数据是不可避免的,所以我们需要常见数据文件(dataset)。在大多数情况下我们通常使用的数据文件是XML格式。我们可以手动的创建一个flat XML dataset文件或者通过导出数据库中的数据来创建一个。
这里简单介绍一下XML dataset 与flat XML dataset:
flat XML dataset相对简介,表中的一行数据表示为:<表名称 字段名=值 ...../>
而XML dataset就相对繁琐了,同样是一行数据其表示为:
<表名称>
<字段名>值</字段名>
.....
</表名称>
1.1 通过数据库数据创建flat XML dataset格式的数据文件
手动创建再次简单说明一下:标准的XML文件头,全部内容包含在<dataset></dataset>标签中,内容格式参考上面说明。
官方提供的示例:
public class DatabaseExportSample
{
public static void main(String[] args) throws Exception
{
// database connection
Class driverClass = Class.forName("org.hsqldb.jdbcDriver");
Connection jdbcConnection = DriverManager.getConnection(
"jdbc:hsqldb:sample", "sa", "");
IDatabaseConnection connection = new DatabaseConnection(jdbcConnection);
// partial database export
QueryDataSet partialDataSet = new QueryDataSet(connection);
partialDataSet.addTable("FOO", "SELECT * FROM TABLE WHERE COL='VALUE'");
partialDataSet.addTable("BAR");
FlatXmlDataSet.write(partialDataSet, new FileOutputStream("partial.xml"));
// full database export
IDataSet fullDataSet = connection.createDataSet();
FlatXmlDataSet.write(fullDataSet, new FileOutputStream("full.xml"));
// dependent tables database export: export table X and all tables that
// have a PK which is a FK on X, in the right order for insertion
String[] depTableNames =
TablesDependencyHelper.getAllDependentTables( connection, "X" );
IDataSet depDataset = connection.createDataSet( depTableNames );
FlatXmlDataSet.write(depDataSet, new FileOutputStream("dependents.xml"));
}
}
根据官方示例自己编写的测试文件:
工具:使用的是MySQL数据库。
public class Ceshi {
public static void main(String[] args) throws Exception {
// database connection 数据库链接
Class.forName("com.mysql.jdbc.Driver");// 加载数据库驱动
Connection jdbcConnection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/firpro", "root", "root");// 获取数据库链接
IDatabaseConnection connection = new DatabaseConnection(jdbcConnection);// 生成DbUnit的数据集的数据库链接
// partial database export 部分数据导出
QueryDataSet partialDataSet = new QueryDataSet(connection);
partialDataSet.addTable("account",
"SELECT ID_ACCOUNT,ACCOUNT FROM ACCOUNT");
partialDataSet.addTable("ac_ro");
FlatXmlDataSet.write(partialDataSet,
new FileOutputStream("partial.xml"));
// full database export 整个数据库导出
IDataSet fullDataSet = connection.createDataSet();
FlatXmlDataSet.write(fullDataSet, new FileOutputStream("full.xml"));
// dependent tables database export: export table X and all tables that
// have a PK which is a FK on X, in the right order for insertion
// 关联表的导出:需要按照插入的正确的顺序导出。例如X表的主键(PK)是其他表的外键(FK),换句话说X表是主表,其他表与其关联。
String[] depTableNames = TablesDependencyHelper
.getAllDependentTables(connection, "account");
IDataSet depDataset = connection.createDataSet(depTableNames);
FlatXmlDataSet.write(depDataset,
new FileOutputStream("dependents.xml"));
}
}
测试结果:数据库中的数据成功导出。
将上面的细节进行更改,以及对生成文件的分析,发现了一些细节问题。
分析结果:
- 导出所有表所生成的文件,主表的数据在从表的下方。
- 导出部分表所生成的文件,主表与从表的数据位置与其执行partialDataSet.addTable()方法的顺序有关。
- 导出关联表所生成的文件,主表的数据在从表的上方。
这一结论也正好印证了我在《DbUnit数据库测试之备份与还原 》中的疑问,在那篇博客中我尝试的解决办法是关闭外键约束检查,不过在此看来是有点鸡肋了。
2.Extend a DBTestCase class
Now you need to create a test class. One way to use Dbunit is to have the test class extend the DBTestCase class. DBTestCase extends the JUnit TestCase class. A template method is required to be implemented: getDataSet() to return the dataset you created in step 1. DBTestCase relies on a IDatabaseTester to do its work, the default configuration uses PropertiesBasedJdbcDatabaseTester, it locates configuration for the DriverManager within the Sytem properties. The simplest way to configure it would be in the constructor of your test class. You may modify this behavior by overriding getDatabaseTester(), using one of the other 3 provided IDatabaseTester implementations or your own.
创建好测试数据文件了,接下来就是创建测试实例了。一种使用DbUnit的方式是继承DBTestCase这个类。DBTestCase继承了JUnit的TestCase类(这是JUni