Created
June 6, 2012 06:04
-
-
Save kirinboy/2880207 to your computer and use it in GitHub Desktop.
A base class of integrate testing using DbUnit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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