Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A base class of integrate testing using DbUnit
public class MyDBTestCaseBase extends DBTestCase {
protected static ApplicationContext ctx;
static {
// init ctx
protected IDataSet getDataSet() throws Exception {
// set column sensing as true, so it can dynamically add columns with null value.
return new FlatXmlDataSetBuilder()
.build(new FileInputStream("data.xml"));
protected IDatabaseTester getDatabaseTester() {
DataSource dataSource = (DataSource) ctx.getBean("dataSource");
IDatabaseTester tester = new DataSourceDatabaseTester(dataSource);
return tester;
protected DatabaseOperation getTearDownOperation() {
return DatabaseOperation.CLEAN_INSERT;
private IDataSet dataSetBackup;
private static final String[] TABLE_NAMES = new String[] { "..." };
protected void setUp() throws Exception {
dataSetBackup = new CachedDataSet(getConnection().createDataSet(TABLE_NAMES));
protected void tearDown() throws Exception {
try {
final IDatabaseTester databaseTester = getDatabaseTester();
assertNotNull( "DatabaseTester is not set", databaseTester );
databaseTester.setTearDownOperation( getTearDownOperation() );
databaseTester.setDataSet( dataSetBackup ); // Using the backup dataset from database instead of getDataSet()
finally {
dataSetBackup = null;
//super.tearDown(); // Do not call base's tearDown
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.