Skip to content

Instantly share code, notes, and snippets.

Embed
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
}
@Override
protected IDataSet getDataSet() throws Exception {
// set column sensing as true, so it can dynamically add columns with null value.
return new FlatXmlDataSetBuilder()
.setColumnSensing(true)
.build(new FileInputStream("data.xml"));
}
@Override
protected IDatabaseTester getDatabaseTester() {
DataSource dataSource = (DataSource) ctx.getBean("dataSource");
IDatabaseTester tester = new DataSourceDatabaseTester(dataSource);
return tester;
}
@Override
protected DatabaseOperation getTearDownOperation() {
return DatabaseOperation.CLEAN_INSERT;
}
private IDataSet dataSetBackup;
private static final String[] TABLE_NAMES = new String[] { "..." };
@Override
protected void setUp() throws Exception {
dataSetBackup = new CachedDataSet(getConnection().createDataSet(TABLE_NAMES));
super.setUp();
}
@Override
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()
databaseTester.setOperationListener(getOperationListener());
databaseTester.onTearDown();
}
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.