Spring / Hibernate / JUnit - No Hibernate Session bound to Thread

http://stackoverflow.com/questions/734614/spring-hibernate-junit-no-hibernate-session-bound-to-thread

Wrong, that will just fill your code with session management code.

First, add a transaction management bean in your context:

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

 The second thing, extend AbstractTransactionalJUnit4SpringContextTests

public class BaseDALTest 
           extends AbstractTransactionalJUnit4SpringContextTests{

 Third thing, annotate you test class with

@TransactionConfiguration
    @Transactional

If your transaction demarcation is correct(surrounding your dao or service) you should be done.

It's not nice to sprinkle session and transaction handling code all around your code (even inside your tests).

相关推荐