La Chamelle
La Chamelle

Reputation: 2967

Testing Spring-JPA

I am developping a web application using Spring (3.1.x), JSF 2, JPA 2 (Hibernate Provider) for tomcat 6.x. I want to test my DAO classes.

In my DAO class: i do this:

@PersistenceContext
private EntityManager entityManager;

In Spring Configuration;

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="OpenPU" />
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

In persistence.xml

<persistence-unit name="OpenPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <non-jta-data-source>java:comp/env/jdbc/mysql_open</non-jta-data-source>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect" />
        <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.transaction.flush_before_completion" value="true"/>
        <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider"/>
        <property name="hibernate.connection.zeroDateTimeBehavior" value="convertToNull"/>
    </properties>
</persistence-unit>

It the first time I make test, and when I test I don't want to use the same persistence unit. I heard about dbunit for using xml data, but i don't understand how to change the persistence unit during the test. Can you help me or give me some example, tutorial.

Thanks you.

Upvotes: 2

Views: 4379

Answers (1)

Grigory Katkov
Grigory Katkov

Reputation: 421

Maybe this tutorial will help. BTW, there is one interesting Spring feature to fit your needs - embedded database support. So, I usually just use following construction to create in-memory H2 db, create schema with schema.sql and fill it with some data from test-data.sql:

<jdbc:embedded-database id="dataSource" type="H2">
    <jdbc:script location="classpath:schema.sql"/>
    <jdbc:script location="classpath:test-data.sql"/>
</jdbc:embedded-database>

Then you could use this bean as datasource for you EntityManagerFactory bean:

<bean id="entityManagerFactory"
   class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
   p:dataSource-ref="dataSource"
   p:persistence-xml-location="classpath:META-INF/persistence.xml">           
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="databasePlatform" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
            <property name="showSql" value="true" />
            <!-- other properties -->
        </bean>
    </property>
    <property name="persistenceUnitName" value="OpenPU" />
</bean>

This is very convenient and concise way to create in-memory database for tests with Spring. (don't forget to add H2 in your classpath) See documentation for details, chapter "13.8 Embedded database support".

Upvotes: 2

Related Questions