开发者

@Transactional Spring Junit test not doing rollback

开发者 https://www.devze.com 2023-03-13 01:41 出处:网络
I try to set up a Junit test case for my dao layer. However, I do NOT want the test data to be actually persisted to the DB.

I try to set up a Junit test case for my dao layer. However, I do NOT want the test data to be actually persisted to the DB.

So I thought I should do it transactionally and rollback after every test. This leaves me with the following datasource setup:

<bean id="dataSource"
    class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"
    p:driverClass="org.postgresql.Driver"
    p:jdbcUrl="jdbc:postgresql://***"
    p:user="***"
    p:password="***/>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
      p:dataSource-ref="dataSource"
      p:packagesToScan="***"
      p:hibernateProperties-ref="hibernateProps" />

<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"
      p:dataSource-ref="dataSource"
      p:sessionFactory-ref="sessionFactory"/>

The to-be-tested dao class is set up as follows:

@Repository
@Transactional
@SuppressWarnings("unchecked")
public class BallotBoxRepositoryHibernateImpl implements BallotBoxRepository {

@Autowired
private SessionFactory sessionFactory;

  @Override
  public void saveVote(DaoObject v) {
    Session sess = sessionFactory.openSession();
    sess.beginTransaction();
    sess.save(v);
    sess.getTransaction().commit();
    sess.close();
  }
[...]
}

The actual persisting job does work nicely. However, the intended Rollback is never made:

INFO main transaction.TransactionalTestExecutionListener:292 - Rolled back transaction after test execution for test context [...]

The TransactionalTextExecutionListener is defined as follows:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/spring/applicationContext.xml"})
@TestExecutionListeners({WebTestExecutionerListener.class, DependencyInjectionTestExecutionListener.class,
TransactionalTestExecutionListener.class})
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
@Transactional
public class DaoTest { ... }

I'm using an autowired member to access my dao methods:

@Autowired
private BallotBoxRepository repo;
开发者_如何学运维

TL;DR

JUnit test case is persisting test data even though it's stating that a rollback has been done.


In your case Spring-based transaction management doesn't work because you manage Hibernate transactions manually.

In order to use Spring-managed transactions you should do the following:

@Override   
public void saveVote(DaoObject v) {
    sessionFactory.getCurrentSession().save(v);
} 

See also:

  • 13.3 Hibernate
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号