Step 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.
Step 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.
You may also use a subclass of DBTestCase, such as one of these:
JdbcBasedDBTestCaseuses a DriverManager to create connections (with the aid of a JdbcDatabaseTester).
DataSourceBasedDBTestCaseuses a javax.sql.DataSource to create connections (with the aid of a DataSourceDatabaseTester).
JndiBasedDBTestCaseuses a javax.sql.DataSource located through JNDI (with the aid of a JndiDatabaseTester).
DefaultPrepAndExpectedTestCaseuses a configurable IDatabaseTester (allowing any connection type) with clear separation of prep and expected datasets.
The following is a sample implementation that returns a connection to a Hypersonic database and a xml dataset:
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 FlatXmlDataSetBuilder().build(new FileInputStream("dataset.xml")); } }
Step 3: (Optional) Implement
getSetUpOperation() and getTearDownOperation() methods
By default, Dbunit performs a CLEAN_INSERT operation before executing each test and performs no cleanup operation afterward. You can modify this behavior by overriding getSetUpOperation() and getTearDownOperation().
The following example demonstrates how you can easily override the operation executed before or after your test.
public class SampleTest extends DBTestCase { ... protected DatabaseOperation getSetUpOperation() throws Exception { return DatabaseOperation.REFRESH; } protected DatabaseOperation getTearDownOperation() throws Exception { return DatabaseOperation.NONE; } ... }
Step 4: (Optional) Override method
setUpDatabaseConfig(DatabaseConfig config)
Use this to change some configuration settings of the dbunit DatabaseConfig.
The following example demonstrates how you can easily override this method:
public class SampleTest extends DBTestCase { ... /** * Override method to set custom properties/features */ protected void setUpDatabaseConfig(DatabaseConfig config) { config.setProperty(DatabaseConfig.PROPERTY_BATCH_SIZE, new Integer(97)); config.setFeature(DatabaseConfig.FEATURE_BATCHED_STATEMENTS, true); } ... }
Step 5: Implement your test
XXX() methods
Implement your test methods as you normally would with JUnit. Your database is now initialized before and cleaned-up after each test methods according to what you did in previous steps.