JdbcBasedDBTestCase
Overview
JdbcBasedDBTestCase
is a DBTestCase subclass
preconfigured to use a
JdbcDatabaseTester —
it is DBTestCase and JdbcDatabaseTester wired together, so you only
implement getDriverClass(), getConnectionUrl(), and getDataSet().
Example
public class AccountTest extends JdbcBasedDBTestCase
{
@Override
protected String getDriverClass()
{
return "org.h2.Driver";
}
@Override
protected String getConnectionUrl()
{
return "jdbc:h2:mem:accountTest;DB_CLOSE_DELAY=-1";
}
@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 getUsername()/getPassword() (both default to null),
and getSetUpOperation()/getTearDownOperation() to change the default
CLEAN_INSERT/NONE operations — see Database
Operations.
When to Use It
Reach for JdbcBasedDBTestCase when a test class inherits its dbUnit setup
directly (no dependency injection) and connects via DriverManager with a
fixed driver/URL. If your test class already extends something else, or you
use DI to supply the connection, use
IDatabaseTester composition instead — with
JdbcDatabaseTester as the field instead of the superclass:
private final IDatabaseTester databaseTester =
new JdbcDatabaseTester("org.h2.Driver", "jdbc:h2:mem:accountTest;DB_CLOSE_DELAY=-1");See the IDatabaseTester guide for the full composition-based pattern.


