那就只能先找到抛出这个异常的位置,然后向前推,逐步找到获取连接的地方。最后发现,连接是在DbUnitTestExecutionListener.prepareDatabaseConnection中获取连接,并且没有做什么进一步的处理,所以前面的设置都不起作用。看来又只能通过重写源代码来达成目的了。
直接上源码吧:
CustomTransactionDbUnitTestExecutionListener类: 完全复制DbUnitTestExecutionListener,只是增加一句代码。注意该类的包路径和DbUnitTestExecutionListener一致。
[java] view plaincopyprint?
private void prepareDatabaseConnection(TestContext testContext, String databaseConnectionBeanName) throws Exception {
Object databaseConnection = testContext.getApplicationContext().getBean(databaseConnectionBeanName);
if (databaseConnection instanceof DataSource) {
databaseConnection = DatabaseDataSourceConnectionFactoryBean.newConnection((DataSource) databaseConnection);
}
Assert.isInstanceOf(IDatabaseConnection.class, databaseConnection);
((IDatabaseConnection)databaseConnection).getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new HsqldbDataTypeFactory());
testContext.setAttribute(CONNECTION_ATTRIBUTE, databaseConnection);
}
private void prepareDatabaseConnection(TestContext testContext, String databaseConnectionBeanName) throws Exception {
Object databaseConnection = testContext.getApplicationContext().getBean(databaseConnectionBeanName);
if (databaseConnection instanceof DataSource) {
databaseConnection = DatabaseDataSourceConnectionFactoryBean.newConnection((DataSource) databaseConnection);
}
Assert.isInstanceOf(IDatabaseConnection.class, databaseConnection);
((IDatabaseConnection)databaseConnection).getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new HsqldbDataTypeFactory());
testContext.setAttribute(CONNECTION_ATTRIBUTE, databaseConnection);
}
绿色就是真正发挥作用的代码。
可是这个类并不能直接饮用,而是通过TransactionDbUnitTestExecutionListener的CHAIN被调用的,而TransactionDbUnitTestExecutionListener同样无法更改,同样只能建一个自定义的TransactionDbUnitTestExecutionListener类,CustomTransactionDbUnitTestExecutionListener:
[java] view plaincopyprint?
public class CustomTransactionDbUnitTestExecutionListener extends TestExecutionListenerChain {
private static final Class[] CHAIN = { TransactionalTestExecutionListener.class,
CustomDbUnitTestExecutionListener.class };
@Override
protected Class[] getChain() {
return CHAIN;
}
}
public class CustomTransactionDbUnitTestExecutionListener extends TestExecutionListenerChain {
private static final Class[] CHAIN = { TransactionalTestExecutionListener.class,
CustomDbUnitTestExecutionListener.class };
@Override
protected Class[] getChain() {
return CHAIN;
}
}
那么测试类的注解也要修改:
[java] view plaincopyprint?
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-test.xml")
@TestExecutionListeners({
DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
CustomTransactionDbUnitTestExecutionListener.class,
ForeignKeyDisabling.class})
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-test.xml")
@TestExecutionListeners({
DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
CustomTransactionDbUnitTestExecutionListener.class,
ForeignKeyDisabling.class})
四、@Transactional标签引起的问题
按照spring-dbunit-test的文档中说法,可以使用@Transactional确保数据的清洁。使用简单,只需要将上面的注解增加一个@Transactional,
[java] view plaincopyprint?
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-test.xml")
@Transactional
@TestExecutionListeners({
DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
CustomTransactionDbUnitTestExecutionListener.class,
ForeignKeyDisabling.class})
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-test.xml")
@Transactional
@TestExecutionListeners({
DependencyInjectionTestExecutionListener.class,
原文转自:http://blog.csdn.net/mydeman/article/details/9374621