Skip to content

Instantly share code, notes, and snippets.

@kirinboy
Created June 6, 2012 06:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirinboy/2880207 to your computer and use it in GitHub Desktop.
Save kirinboy/2880207 to your computer and use it in GitHub Desktop.
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