How to setup Spring Batch with DbUnit?

347 views Asked by At

I'm trying to test spring batch using dbunit but when batch runs HibernateCursorItemReader it doesn't see temporary created objects in the db. But if I create entities using HibernateDaoSupport everything is fine and I can work with test data. Does anybody know how to force batch see my test entities?

My test classes:

@RunWith(SpringEasyMockRunner.class)
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class, DbUnitTestExecutionListener.class})
@Transactional
@ContextConfiguration({"classpath:/integration-test-context.xml"})
@DbUnitConfiguration(
    databaseConnection = {"dbUnitDatabaseConnection"}
)
public abstract class BaseIT implements ApplicationContextAware {
...
}
@RunWith(SpringEasyMockRunner.class)
@ContextConfiguration("classpath:/sopl-test-context.xml")
@DatabaseSetup(value = "classpath:data/SoplTestDataIT.xml", type = DatabaseOperation.CLEAN_INSERT)
@Slf4j
public class SoplOrderTestIT extends BaseIT {
...
}

And configs integration-test-context.xml:

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" lazy-init="true" destroy-method="close">
        <property name="driverClass" value="${hibernate.driver}"/>
        <property name="jdbcUrl" value="${hibernate.connection.url}"/>
        <property name="user" value="${hibernate.connection.username}"/>
        <property name="password" value="${hibernate.connection.password}"/>
        <property name="initialPoolSize" value="${c3p0.initialPoolSize:1}"/>
        <property name="minPoolSize" value="${c3p0.minPoolSize:0}"/>
        <property name="maxPoolSize" value="${c3p0.maxPoolSize:200}"/>
        <property name="maxIdleTime" value="${c3p0.maxIdleTime:120}"/>
        <property name="maxStatementsPerConnection" value="${c3p0.maxStatementsPerConnection:100}"/>
        <property name="checkoutTimeout" value="${c3p0.checkoutTimeout:30000}"/>
        <property name="idleConnectionTestPeriod" value="${c3p0.idleConnectionTestPeriod:3600}"/>
        <property name="unreturnedConnectionTimeout" value="${c3p0.unreturnedConnectionTimeout:7200}"/>
        <property name="debugUnreturnedConnectionStackTraces"
                  value="${c3p0.debugUnreturnedConnectionStackTraces:true}"/>
    </bean>

    <bean name="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="namingStrategy">
            <bean class="org.hibernate.cfg.DefaultComponentSafeNamingStrategy"/>
        </property>
        <property name="configLocations" value="classpath:/hibernate/*-hibernate.cfg.xml"/>
        <property name="hibernateProperties">
            <props merge="true">
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
                <prop key="hibernate.cache.use_second_level_cache">false</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql:false}</prop>
                <prop key="hibernate.format_sql">${hibernate.format_sql:false}</prop>
                <prop key="hibernate.use_sql_comments">${hibernate.use_sql_comments:false}</prop>
                <prop key="hibernate.generate_statistics">${hibernate.generate_statistics:false}</prop>
                <prop key="hibernate.hbm2ddl.auto"/>
                <prop key="hibernate.search.autoregister_listeners">false</prop>
            </props>
        </property>
    </bean>

    <!-- transaction manager for hibernate -->
    <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" primary="true">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <!-- spring-test-dbunit configuration: -->
    <bean id="dbUnitDatabaseConfig" class="com.github.springtestdbunit.bean.DatabaseConfigBean">
        <property name="qualifiedTableNames" value="true"/>
        <property name="metadataHandler">
            <bean class="org.dbunit.ext.mysql.MySqlMetadataHandler"/>
        </property>
        <property name="datatypeFactory">
            <bean class="org.dbunit.ext.mysql.MySqlDataTypeFactory"/>
        </property>
    </bean>
    <bean id="dbUnitDatabaseConnection"
          class="com.github.springtestdbunit.bean.DatabaseDataSourceConnectionFactoryBean">
        <property name="databaseConfig" ref="dbUnitDatabaseConfig"/>
        <property name="dataSource" ref="dataSource"/>
    </bean>
...
0

There are 0 answers