views:

57

answers:

1

I'm getting a LazyInitializationException trying to test my DAO methods using the tool stack defined in the title. My understanding is that my test must be running outside the hibernate session, or it has been closed before I try to read children objects from my DAO. From reading the documentation, I understood that using the @TransactionConfiguration tag would allow me to define the transaction manager in which to run the tests.

I've read the documentation multiple times and a zillion forum posts. Still slamming my head into my keyboard... What am I missing? Thanks for your help!

my unit test class:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = {
      "classpath:/WEB-INF/applicationContext-db.xml",
      "classpath:/WEB-INF/applicationContext-hibernate.xml",
      "classpath:/WEB-INF/applicationContext.xml" })
    @TestExecutionListeners({DependencyInjectionTestExecutionListener.class, CleanInsertTestExecutionListener.class})
    @DataSetLocation("test/java/com/yada/yada/dao/dbunit-general.xml")
    @TransactionConfiguration(transactionManager="transactionManager", defaultRollback = true)
    @Transactional
    public class RealmDAOJU4Test {

     @Autowired
     private DbUnitInitializer dbUnitInitializer;

     @Autowired
     private RealmDAO realmDAO;

     @Test
     public void testGetById() {
      Integer id = 2204;
      Realm realm = realmDAO.get(id);
      assertEquals(realm.getName().compareToIgnoreCase(
        "South Technical Realm"), 0);
      assertEquals(8, realm.getRealmRelationships().size());
     }
}

my applicationContext-hibernate.xml:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource" ref="dataSource"></property>
  <property name="useTransactionAwareDataSource" value="true" />
   ... other properties removed ...
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory">
   <ref bean="sessionFactory" />
  </property>
</bean>

my dao definition in applicationContext.xml

<bean id="realmDAOTarget" class="com.yada.yada.dao.hibernate.RealmDAOImpl">
  <property name="sessionFactory" ref="sessionFactory" />
 </bean>
 <bean id="realmDAO" class="org.springframework.aop.framework.ProxyFactoryBean">
  <property name="proxyInterfaces">
   <value>com.yada.yada.dao.RealmDAO</value>
  </property>
  <property name="interceptorNames">
   <list>
    <value>hibernateInterceptor</value>
    <value>realmDAOTarget</value>
   </list>
  </property>
 </bean>
+1  A: 

well, for anyone following along at home, here's what I missed:

TransactionalTestExecutionListener

it is required in the @TestExecutionListeners list for the @Transactional annotation to have any effect.

tia