Test Integration
Overview
dbUnit offers two ways to wire its setup/teardown lifecycle into a test class:
- Composition — a test class holds an IDatabaseTester field, constructed directly or dependency-injected.
- Inheritance — a test class extends
DBTestCase (or one of its
three subclasses below), which owns an
IDatabaseTesterinternally.
Composition is the preferred style — it doesn’t compete for your test
class’s one available superclass, and fits naturally with dependency
injection (Spring or otherwise). See the
IDatabaseTester guide for the interface hierarchy, lifecycle, and a complete
example. Inheritance remains fully supported for tests that prefer it, or
that predate this recommendation; see
Migrating to IDatabaseTester
if you want to convert an existing DBTestCase subclass.
On JUnit 5/6 (Jupiter), composition users can also skip writing the
@BeforeEach/@AfterEach lifecycle calls entirely — see
DbUnitExtension.
Whichever style you choose,
PrepAndExpectedTestCase is
usually a better starting point than driving either one directly — it
packages prep/verify/cleanup behind runTest(), needs no parent class, and
works with both styles (it holds an IDatabaseTester, so it composes either
way).
Inheritance: DBTestCase and Its Subclasses
DBTestCase extends the top-level DatabaseTestCase. A template method,
getDataSet(), is required to be implemented, returning the dataset for the
test. DBTestCase relies on an IDatabaseTester to do its work; the default
configuration uses PropertiesBasedJdbcDatabaseTester, which locates
DriverManager configuration in System properties. The simplest way to
configure it is in the constructor of your test class. You may modify this
behavior by overriding newDatabaseTester(), using one of the subclasses
below or your own IDatabaseTester.
| Class | Uses |
|---|---|
| JdbcBasedDBTestCase | A DriverManager connection, via JdbcDatabaseTester. |
| DataSourceBasedDBTestCase | A javax.sql.DataSource, via DataSourceDatabaseTester. |
| JndiBasedDBTestCase | A javax.sql.DataSource located through JNDI, via JndiDatabaseTester. |
The following sample implementation returns a connection to an H2 in-memory database and an XML dataset:
public class SampleTest extends DBTestCase
{
public SampleTest(String name)
{
super(name);
System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_DRIVER_CLASS, "org.h2.Driver");
System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_CONNECTION_URL, "jdbc:h2:mem:sample;DB_CLOSE_DELAY=-1");
System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_USERNAME, "sa");
System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_PASSWORD, "");
}
@Override
protected IDataSet getDataSet() throws Exception
{
return new FlatXmlDataSetBuilder().build(new FileInputStream("dataset.xml"));
}
@BeforeEach
@Override
protected void setUp() throws Exception
{
super.setUp();
}
@AfterEach
@Override
protected void tearDown() throws Exception
{
super.tearDown();
}
}Changing the Setup/Teardown Operation
By default, dbUnit performs a CLEAN_INSERT
operation before executing each test and no cleanup operation afterward. You
can modify this behavior by overriding getSetUpOperation() and
getTearDownOperation():
public class SampleTest extends DBTestCase
{
...
@Override
protected DatabaseOperation getSetUpOperation() throws Exception
{
return DatabaseOperation.REFRESH;
}
@Override
protected DatabaseOperation getTearDownOperation() throws Exception
{
return DatabaseOperation.DELETE_ALL;
}
...
}Customizing DatabaseConfig
Override setUpDatabaseConfig(DatabaseConfig config) to change dbUnit
configuration settings for the connection:
public class SampleTest extends DBTestCase
{
...
@Override
protected void setUpDatabaseConfig(DatabaseConfig config)
{
config.setProperty(DatabaseConfig.PROPERTY_BATCH_SIZE, 97);
config.setFeature(DatabaseConfig.FEATURE_BATCHED_STATEMENTS, true);
}
...
}General Notes
- For additional examples, refer to the ITs for the specific test case.
- To change the setup or teardown operation (e.g. change the teardown to
org.dbunit.operation.DatabaseOperation.DELETE_ALL), set thesetUpOperationortearDownOperationproperty on the databaseTester — see Database Operations. - To set
DatabaseConfigfeatures/properties, one way is to extend the test case class and override thesetUpDatabaseConfig(DatabaseConfig config)method fromDatabaseTestCase, as shown above.


