DataSourceBasedDBTestCase
Overview
DataSourceBasedDBTestCase
is a DBTestCase subclass
preconfigured to use a
DataSourceDatabaseTester —
it is DBTestCase and DataSourceDatabaseTester wired together, so you only
implement getDataSource() and getDataSet().
Example
public class AccountTest extends DataSourceBasedDBTestCase
{
@Override
protected DataSource getDataSource()
{
return myApplicationDataSource; // e.g. injected or looked up
}
@Override
protected IDataSet getDataSet() throws Exception
{
return new FlatXmlDataSetBuilder()
.build(getClass().getResourceAsStream("/dbunit/account-prep.xml"));
}
@BeforeEach
@Override
protected void setUp() throws Exception
{
super.setUp();
}
@AfterEach
@Override
protected void tearDown() throws Exception
{
super.tearDown();
}
@Test
void testWithdraw_sufficientBalance_decrementsBalance() throws Exception
{
// exercise the code under test, then assert with getConnection()
// or Assertion.assertEquals() — see Data Comparisons
}
}Optionally override getSetUpOperation()/getTearDownOperation() to change
the default CLEAN_INSERT/NONE operations — see
Database Operations.
When to Use It
Reach for DataSourceBasedDBTestCase when a test class inherits its dbUnit
setup directly (no dependency injection) and a javax.sql.DataSource is
already available to obtain (e.g. from a connection pool or an application
context lookup). If your test class already extends something else, or you
use DI to inject the DataSource/tester directly, use
IDatabaseTester composition instead — with
DataSourceDatabaseTester as the field instead of the superclass:
private final IDatabaseTester databaseTester =
new DataSourceDatabaseTester(myApplicationDataSource);See the IDatabaseTester guide for the full composition-based pattern.


