DBUnit:如何仅在失败时刷新?
我正在使用 DBUnit 来测试 Spring/Hibernate 持久性。
我创建了一个抽象测试:
public abstract class AbstractTestCase extends
AbstractTransactionalDataSourceSpringContextTests {
@Override
protected String[] getConfigLocations() {
return new String[] {
"classpath:/applicationContext.xml",
"classpath:/testDataSource.xml" };
}
@Override
protected void onSetUpInTransaction() throws Exception {
DataSource dataSource = jdbcTemplate.getDataSource();
Connection con = DataSourceUtils.getConnection(dataSource);
boolean validateSchemaExists = true;
IDatabaseConnection dbUnitCon = new DatabaseConnection(con,
"MHADB_TEST", validateSchemaExists);
DatabaseConfig dbUnitConConfig = dbUnitCon.getConfig();
dbUnitConConfig.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY,
new OracleDataTypeFactory());
boolean enableColumnSensing = true;
boolean enableDTDMetadata = false;
IDataSet dataSet = new FlatXmlDataSet(new File(
"./src/test/resources/mhadb-dataset.xml"),
enableDTDMetadata,
enableColumnSensing);
try {
DatabaseOperation.CLEAN_INSERT.execute(dbUnitCon, dataSet);
} finally {
DataSourceUtils.releaseConnection(con, dataSource);
}
}
}
但是每当我运行测试时,它都会在每个测试方法之前再次刷新数据库数据集,我认为这非常长而且非常不好。
如何仅在失败时重新加载/刷新?
PS:代码有多糟糕?
I am using DBUnit to test a Spring/Hibernate persistence.
I created an abstract test:
public abstract class AbstractTestCase extends
AbstractTransactionalDataSourceSpringContextTests {
@Override
protected String[] getConfigLocations() {
return new String[] {
"classpath:/applicationContext.xml",
"classpath:/testDataSource.xml" };
}
@Override
protected void onSetUpInTransaction() throws Exception {
DataSource dataSource = jdbcTemplate.getDataSource();
Connection con = DataSourceUtils.getConnection(dataSource);
boolean validateSchemaExists = true;
IDatabaseConnection dbUnitCon = new DatabaseConnection(con,
"MHADB_TEST", validateSchemaExists);
DatabaseConfig dbUnitConConfig = dbUnitCon.getConfig();
dbUnitConConfig.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY,
new OracleDataTypeFactory());
boolean enableColumnSensing = true;
boolean enableDTDMetadata = false;
IDataSet dataSet = new FlatXmlDataSet(new File(
"./src/test/resources/mhadb-dataset.xml"),
enableDTDMetadata,
enableColumnSensing);
try {
DatabaseOperation.CLEAN_INSERT.execute(dbUnitCon, dataSet);
} finally {
DataSourceUtils.releaseConnection(con, dataSource);
}
}
}
But whenever I run a test, it refresh the DB agains the dataSet before EVERY test method, which is quite very long and very not nice I think.
How could I reload/refresh only on failure?
P.S.: How much does the code suck?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了一种加快测试速度的方法,即使行为仍然不符合预期(失败时刷新):
I found a way to speed up the tests, even thought the behaviour is still not as expected (refresh on fail):