Dbunit introduction and usage

Source: Internet
Author: User
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 "));
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.