junit 4 在 Eclipse 中使用 spring 3.0 和 Hibernate 3 进行测试 - LazyInitializationException

发布于 2024-09-26 09:10:06 字数 2475 浏览 4 评论 0原文

我尝试使用标题中定义的工具堆栈测试我的 DAO 方法时遇到 LazyInitializationException。我的理解是,我的测试必须在休眠会话之外运行,或者在我尝试从 DAO 读取子对象之前它已关闭。通过阅读文档,我了解到使用 @TransactionConfiguration 标签将允许我定义运行测试的事务管理器。

我已经多次阅读了文档和无数的论坛帖子。仍然把头撞在键盘上......我错过了什么?感谢您的帮助!

我的单元测试类:

    @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());
     }
}

我的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>

我的dao定义在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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伤感在游骋 2024-10-03 09:10:06

好吧,对于在家里关注的人来说,这就是我错过的:

TransactionalTestExecutionListener

@TestExecutionListeners 列表中需要它才能使 @Transactional 注释产生任何效果。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文