Spring @Transactional does not work in JUnit test?

Nilesh picture Nilesh · May 23, 2012 · Viewed 28.7k times · Source

I am using Spring 3.2, Hibernate and JUnit 4.

My Dao class is as follows:

@Transactional public class SomeDaoImpl implements SomeDao {

The update operations on this work if executed directly from web application. However, I am seeing that junit integration tests that exercise the update methods do not actually persist the changes. Is something rolling the transactions back when junit methods are executed?

Answer

nobeh picture nobeh · May 23, 2012

By reference, transactions are not persisted in test contexts in Spring. As mentioned, although unusual, if you still need to do so you can use @TransactionConfiguration and @Rollback to change the default behavior.