springunit-testingh2liquibaseliquibase-hibernate

Embedded H2 Database for Spring unit test with Liquibase: No data in tables when running test


I have embedded H2 database for my Spring unit tests. The tables and data should be initialized with Liquibase. However, when a test is running, there are no data in the table.

I'm using Spring 4.2.1, Liquibase 4.7.1 and H2 2.1.210.

Below is my implementation:

The datasource is declare in .xml file

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.h2.Driver" />
        <property name="url" value="jdbc:h2:mem:test;MODE=Oracle;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE" />
        <property name="username" value="test" />
        <property name="password" value="test" />
    </bean>

    <bean id="liquibase" class="liquibase.integration.spring.SpringLiquibase">
        <property name="dataSource"  ref="dataSource"  />
        <property name="changeLog"  value="classpath:liquibase/changelog/sil_client_init.h2.sql"  />
        <property name="contexts"  value="test"  />
    </bean>

    <!-- Session factory -->
    <bean id="sessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:hibernate-config.xml" />
    </bean>

    <!-- Transaction manager -->
    <bean id="txManager"
          class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

The sil_client_init.h2.sql file contains SQL contains changeset for creating and inserting several data in the table.

I have a super class which load the context, including the "liquibase" bean declared above.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:applicationContext.xml" })
@Ignore
public class SpringApplicationContextIT {

    @Before
    public void setUp() {
        BasicConfigurator.configure();
        FacesContextMocker.mockFacesContext();
        assertNotNull(applicationContext);
    }

    @Autowired
    private ApplicationContext applicationContext;

}

From which my test class inherits

public class ClientDAOTest extends SpringApplicationContextIT {

    @Autowired
    private IClientDAO simpleClientDAO;

    @Test
    @Transactional
    public void getAllClients() throws ClientDAOException {
        simpleClientDAO.find();
    }

When running getAllClients(), the context are loaded and there is log showing that my changesets Liquibase were running. However, when the test arrives at simpleClientDAO.find(), my Client table is empty.

Any idea of how to fix, how this could happen or how I could investigate the reason would be appreciated. Thank you so much in advance!


Solution

  • For those who might have the same problem, turn out the datasource is properly loaded but my sessions are not correctly connected. I had to specify a package to scan for my sessionFactory bean:

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
           <property name="packagesToScan">
             <value>com.myApp.dao.entity</value>
           </property>       
        <property name="configLocation" value="/WEB-INF/classes/hibernate-config.xml" />
    </bean>