1

我有如下测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:META-INF/spring/testDataSpringContext.xml" })
@Transactional
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class,
    TransactionDbUnitTestExecutionListener.class })
public class AgenceDAOTest {

    @Autowired
    private AgenceDAO mAgenceDAO;

    @Test
    @DatabaseSetup(value = "/META-INF/db-test/sampleData.xml", type = DatabaseOperation.REFRESH)
    public void listAgences() {
        List<AgenceVO> vListeAgences = mAgenceDAO.getAgences();

        Assert.notNull(vListeAgences);
        Assert.notEmpty(vListeAgences);

        List<AgenceVO> vListeAgencesTrouvees = ListUtils.select(vListeAgences, new Predicate<AgenceVO>() {
            public boolean evaluate(AgenceVO pAgenceVO) {
                return pAgenceVO.getLibelle().startsWith("TEST_");
            }
        });

        Assert.notNull(vListeAgencesTrouvees);
        Assert.notEmpty(vListeAgencesTrouvees);
        Assert.isTrue(vListeAgencesTrouvees.size() == 1);
    }
}

一切似乎都很好,因为在日志中我看到以下内容:

[TransactionalTestExecutionListener: startNewTransaction];Began transaction (1): transaction manager [org.springframework.jdbc.datasource.DataSourceTransactionManager@39d325]; rollback [true]
[DbUnitTestExecutionListener: setupOrTeardown];Executing Setup of @DatabaseTest using REFRESH on /META-INF/db-test/sampleData.xml
[AbstractTableMetaData: getDataTypeFactory];Potential problem found: The configured data type factory 'class org.dbunit.dataset.datatype.DefaultDataTypeFactory' might cause problems with the current database 'Oracle' (e.g. some datatypes may not be supported properly). In rare cases you might see this message because the list of supported database products is incomplete (list=[derby]). If so please request a java-class update via the forums.If you are using your own IDataTypeFactory extending DefaultDataTypeFactory, ensure that you override getValidDbProducts() to specify the supported database products.
[SQL: logStatement];select this_.AGC_ID as AGC1_0_0_, this_.AGC_CP as AGC2_0_0_, this_.AGC_ADR1 as AGC3_0_0_, this_.AGC_COMMUNE as AGC4_0_0_, this_.AGC_ADR2 as AGC5_0_0_, this_.AGC_LIBELLE as AGC6_0_0_, this_.AGC_MAIL as AGC7_0_0_, this_.AGC_NOM as AGC8_0_0_, this_.AGC_TEL as AGC9_0_0_ from FTN_AGENCE_AGC this_
[DbUnitTestExecutionListener: verifyExpected];Skipping @DatabaseTest expectation due to test exception class java.lang.IllegalArgumentException
[TransactionalTestExecutionListener: endTransaction];Rolled back transaction after test execution for test context [[TestContext@cdd54e testClass = AgenceDAOTest, locations = array<String>['classpath:META-INF/spring/testDataSpringContext.xml'], testInstance = com.edf.ftn.data.admin.AgenceDAOTest@16f2067, testMethod = listAgences@AgenceDAOTest, testException = java.lang.IllegalArgumentException: [Assertion failed] - this collection must not be empty: it must contain at least 1 element]]

dbunit 数据集在事务创建后加载,因此数据集数据在 select 中应该可见,但不可见。执行选择时,不会检索数据集中的记录。

为了验证是否正在加载数据集,我尝试插入重复键并启动了异常,因此我假设数据集已正确加载。

数据源和事务管理器配置为:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
    <property name="url" value="jdbc:oracle:thin:@${ip}:${port}:${schema}" />
    <property name="username" value="${user}" />
    <property name="password" value="${pass}" />
    <property name="defaultAutoCommit" value="false" />
</bean>

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>

DAO 未配置为事务性的,因为在应用程序中它不是。但我也尝试过使其具有事务性,结果是一样的。

我不明白为什么在这一行:

List<AgenceVO> vListeAgences = mAgenceDAO.getAgences();

数据集不可见。


找到解决方案

我通过使用解决了这个问题TransactionAwareDataSourceProxy

最后我得到了数据源的以下配置:

<bean id="dbcpDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
    <property name="url" value="jdbc:oracle:thin:@${ip}:${port}:${schema}" />
    <property name="username" value="${user}" />
    <property name="password" value="${pass}" />
    <property name="defaultAutoCommit" value="false" />
</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
    <constructor-arg ref="dbcpDataSource" />
</bean>

<bean id="futunoaTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>
4

0 回答 0