1

I have a test like the following

@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);
    }
}

Everything seems ok because in the log I see the following:

[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]]

The dbunit dataset is loaded after the transaction is created, so dataset data should be visible in select, but it is not visible. When the select is executed records in the dataset are not retrieved.

To verify if the dataset is being loaded I've tried to insert a duplicate key and an exception is launched, so I supose that de dataset is loaded correctly.

The datasource and transactionmanager configuration is:

<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>

The DAO is not configured as transactional, because in the application it isn't. But I have also tried to make it transactional, and the result is the same.

I do not understand why in this line:

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

The dataset is not visible.


Solution found

I fixed the problem by using TransactionAwareDataSourceProxy

Finally I've got the following configuration for datasource:

<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>
Community
  • 1
  • 1
qopuir
  • 369
  • 1
  • 5
  • 20

0 Answers0