Official Website: http://www.dbunit.org/
Official introduction:
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 when upts 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.
1. dbunit cannot identify the BIT data type of mysql5. Therefore, you need to write a new datatypefactory to process the BIT data type.
Package net. Jim. database. dbunit;
Import java. SQL. types;
Import org. dbunit. dataset. datatype. datatype;
Import org. dbunit. dataset. datatype. datatypeexception;
Import org. dbunit. Ext. MySQL. mysqldatatypefactory;
Public class mysql5datatypefactory extends mysqldatatypefactory {
Public datatype createdatatype (INT sqltype, string sqltypename)
Throws datatypeexception {
If (sqltype = types. Other ){
// Boolean
If ("bit". Equals (sqltypename )){
Return datatype. boolean;
}
}
Return super. createdatatype (sqltype, sqltypename );
}
}
2. Expand dbunit's databasetestcase class to implement the getconnection () and getdataset () methods. In this way, you only need to implement this abstract class when writing test cases.
Package net. Jim. Site. dbunit;
Import java. Io. fileinputstream;
Import net. Jim. database. dbunit. mysql5datatypefactory;
Import net. Jim. Site. util. testutil;
Import org. dbunit. database. databaseconfig;
Import org. dbunit. database. databaseconnection;
Import org. dbunit. database. idatabaseconnection;
Import org. dbunit. dataset. idataset;
Import org. dbunit. dataset. xml. xmldataset;
Public abstract class databasetestcase extends org. dbunit. databasetestcase {
@ override
protected idatabaseconnection getconnection () throws exception {
idatabaseconnection connection = new databaseconnection (testutil
. getconnection ();
databaseconfig Config = connection. getconfig ();
config. setproperty (databaseconfig. property_datatype_factory,
New mysql5datatypefactory ();
return connection;
}
@ Override
Protected idataset getdataset () throws exception {
Return new xmldataset (New fileinputstream (
"Src/test/NET/Jim/site/dbunit/sitedata. xml "));
}
}