Apache Ant is a Java-based build tool. In theory, it is kind of like Make, but without Make's wrinkles.
DbUnit:http://www.dbunit.org/
DbUnit is a JUnit extension (also usable with Ant) targeted for database-driven projects that, among other things, puts your database into a known state between test runs. This is an excellent way to avoid the myriad of problems that can occur when one test case corrupts the database and causes subsequent tests to fail or exacerbate the damage.
DbUnit has the ability to export and import your database data to and from XML datasets. Since version 2.0, DbUnit can works with very large dataset when use in streaming mode. DbUnit can also helps you to verify that your database data match expected set of values.
JUnitDoclet:http://www.junitdoclet.org/
Why wasn't this tested before? This question often arises shortly before a deadline. Our answer: Because testing was not easy enough.
Especially creating and maintaining a test structure is often used as an excuse. JUnitDoclet lowers the step toward JUnit. It generates skeletons of TestCases based on your application source code. And it supports you to reorganize tests during refactoring. Suddenly all the excuses don't count any longer.
MySQL数据库:Pagination,表:Persioninfo信息如下:
# Host: localhost
# Database: pagination
# Table: 'personinfo'
#
CREATE TABLE `personinfo` (
`id` varchar(100) NOT NULL default '',
`name` varchar(50) NOT NULL default '',
`sex` tinyint(1) NOT NULL default '0',
`mobile` varchar(50) NOT NULL default '0',
`address` varchar(50) NOT NULL default '',
`memo` varchar(50) default '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=gb2312;
一、用Ant的任务来实现
1、DataSelt.xml内容如下:
<dataset><?xml version="1.0" encoding="GBK"?>
<dataset>
<Personinfo id="9001" name="YuLimin" sex="0" mobile="13800138000" address="中国福建莆田" memo="Ant DBUnit Test"/>
<Personinfo id="9002" name="俞Limin" sex="1" mobile="13800138000" address="中华人民共和国" memo="Ant DBUnit Test"/>
<Personinfo id="9003" name="俞黎敏" sex="0" mobile="13800138000" address="中华人民共和国福建" memo="Ant DBUnit Test"/>
</dataset>
2、Ant 的 build.xml内容如下,请进行相应的改动:
<project name="DbUnitTest" default="DbUnit" basedir="."><?xml version="1.0" encoding="GBK"?>
<project name="DbUnitTest" basedir="." default="initdb">
<!-- 定义DbUnit需要的包,包括DbUnit包和jdbc包。 -->
<property name="lib.dir" value="E:\OpenSource"/>
<property name="dbunit.jar" value="${lib.dir}\DBUnit\dbunit-2.1.jar"/>
<property name="jdbc.jar" value="${lib.dir}\MySQL\lib\mysql-connector-java-3.1.7-bin.jar"/>
<property name="junit.jar" value="${lib.dir}\JUnit\junit.jar"/>
<property name="junitdoclet.jar" value="${lib.dir}\JUnitDoclet\JUnitDoclet.jar"/>
<property name="classes.dir" value="./classes"/>
<!-- 在全局属性定义中定义数据库连接的url,driver,userid,password,进行多个操作可以达到重用 -->
<property name="dburl" value="jdbc:mysql://localhost/Pagination"/>
<property name="dbdriver" value="com.mysql.jdbc.Driver"/>
<property name="dbuserid" value="root"/>
<property name="dbpwd" value="password"/>
<path id="DbUnit.classpath">
<pathelement location="${dbunit.jar}"/>
<pathelement location="${jdbc.jar}"/>
<pathelement location="${junit.jar}"/>
<pathelement location="${junitdoclet.jar}"/>
</path>
<target name="init">
<!-- 定义DbUnit的Ant任务类 -->
<taskdef name="dbunit" classname="org.dbunit.ant.DbUnitTask" classpathref="DbUnit.classpath"/>
</target>
<target name="initdb" depends="init">
<dbunit driver="${dbdriver}" supportBatchStatement="false" url="${dburl}" userid="${dbuserid}" password="${dbpwd}" classpathref="DbUnit.classpath">
<!-- operation的类型有:UPDATE 、INSERT、DELETE 、DELETE_ALL 、TRUNCATE 、REFRESH(MSSQL_REFRESH)、CLEAN_INSERT(MSSQL_CLEAN_INSERT)、NONE -->
<!-- (注意:MSSQLServer中在CLEAN_INSERT和REFRESH中要使用后面的MSSQL_REFRESH和MSSQL_CLEAN_INSERT) -->
<!-- INSERNT仅插入数据 -->
<!-- CLEAN_INSERT清除并插入数据 -->
<operation type="INSERT" src="DataSet.xml"/>
</dbunit>
</target>
<target name="export" depends="init">
<!-- 导出全部包含有数据的表的数据出来 -->
<dbunit driver="${dbdriver}" supportBatchStatement="false" url="${dburl}" userid="${dbuserid}" password="${dbpwd}" classpathref="DbUnit.classpath">
<export dest="Export.xml"/>
</dbunit>
</target>
<target name="select" depends="init">
<!-- 导出一个由查询语句产生的数据集和指定的一个表的数据 -->
<dbunit driver="${dbdriver}" supportBatchStatement="false" url="${dburl}" userid="${dbuserid}" password="${dbpwd}" classpathref="DbUnit.classpath">
<export dest="Select.xml">
<query name="Personinfo" sql="Select * From Personinfo Where name='YuLimin'"/>
<table name="Personinfo"/>
</export>
</dbunit>
</target>
</project>
3、运行:ant或ant initdb或ant export或ant select不同的任务
二、直接写DbUnit的测试代码实现,DbUnitTest.java的代码如下:
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import org.dbunit.DatabaseTestCase;
import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSet;
import org.dbunit.operation.DatabaseOperation;
/**
*
Title: 运行DbUnit测试将数据导入数据库。
*
*
Description: 运行DbUnit测试将数据导入数据库。
*
*
Copyright: Copyright (c) 2004
*
*
Company: Beyond DayBreak Office
* @author YuLimin
* @version 1.0
*/
public class DbUnitTest extends DatabaseTestCase
{
public DbUnitTest(String name)
{
super(name);
}
/**
*
* @see org.dbunit.DatabaseTestCase#getConnection()
* @throws Exception
* @return IDatabaseConnection
*/
protected IDatabaseConnection getConnection() throws Exception
{
Class driverClass = Class.forName("com.mysql.jdbc.Driver");
Connection jdbcConnection = DriverManager.getConnection("jdbc:mysql://localhost/Pagination","root","password");
return new DatabaseConnection(jdbcConnection);
}
/**
*
* @see org.dbunit.DatabaseTestCase#getDataSet()
* @throws Exception
* @return IDataSet
*/
protected IDataSet getDataSet() throws Exception
{
return new FlatXmlDataSet(new FileInputStream("DataSet.xml"));
}
/**
* getSetUpOperation
*
* @return DatabaseOperation
* @throws Exception
*/
protected DatabaseOperation getSetUpOperation() throws Exception
{
return DatabaseOperation.REFRESH;
}
/**
* getTearDownOperation
*
* @return DatabaseOperation
* @throws Exception
*/
protected DatabaseOperation getTearDownOperation() throws Exception
{
return DatabaseOperation.NONE;
}
/**
* 运行DbUnit测试将数据导入数据库。
*/
public void testDbUnit()
{
System.out.println("DbUnit Testing...");
}
}