I am trying to set up a Junit test case for my dao level. However, I do NOT want the test data to actually be stored in the database.
So I thought I should do it transactionally and roll back after each test. This leaves me with the following data source 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 checked dao class is configured 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 persistent job works well. However, the intended rollback is never performed:
INFO main transaction.TransactionalTestExecutionListener:292 - Rolled back transaction after test execution for test context [...]
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 use an authorized element to access my dao methods:
@Autowired
private BallotBoxRepository repo;
TL DR
The JUnit test case saves the test data, even if it states that the rollback is complete.