Simple Spring Transactional JUnit 4 Test

David Winterfeldt

2008


Simple Spring Transactional JUnit 4 Test of a Hibernate transaction.

1. Spring Configuration

A transactionManager bean is setup for the transactional annotations to use and the DataSource from the shared context (not shown) is used by AbstractTransactionalJUnit4SpringContextTests to make a SimpleJdbcTemplate available.

PersonDaoTransactionUnitTest-context.xml
                
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
	   					   http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx.xsd">

    <import resource="classpath:org/springbyexample/orm/hibernate3/shared-context.xml"/>

    <bean id="transactionManager" 
          class="org.springframework.orm.hibernate3.HibernateTransactionManager"
          p:sessionFactory-ref="sessionFactory" />

    <tx:annotation-driven/>
   
    <bean id="personDao" 
          class="org.springbyexample.orm.hibernate3.dao.PersonDaoImpl"
          p:sessionFactory-ref="sessionFactory" />
 
</beans>