I'm running a simple JUnit test agains an application DAO. The problem is that I always get:
javax.persistence.RollbackException: Transaction marked as rollbackOnly
The JUnit test is:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:com/my/app/context.xml"} @TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false) @Transactional public class PerformanceTest { @Test @Transactional(propagation= Propagation.REQUIRES_NEW) @Rollback(false) public void testMsisdnCreationPerformance() { // Create a JPA entity // Persist JPA entity } }
As you can see I'm declaring clearly not to rollback this method.
Does Spring JUnit support always sets rollback to true?
Annotation Type Rollback@Rollback is a test annotation that is used to indicate whether a test-managed transaction should be rolled back after the test method has completed.
The @Transactional annotation makes use of the attributes rollbackFor or rollbackForClassName to rollback the transactions, and the attributes noRollbackFor or noRollbackForClassName to avoid rollback on listed exceptions. The default rollback behavior in the declarative approach will rollback on runtime exceptions.
Rolls back an explicit or implicit transaction to the beginning of the transaction, or to a savepoint inside the transaction. You can use ROLLBACK TRANSACTION to erase all data modifications made from the start of the transaction or to a savepoint. It also frees resources held by the transaction.
Rollback: is used to maintain database integrity, rollbacks are performed when system operations become erroneous. In worse case scenarios, rollback strategies are used for recovery when the structure crashes. A clean copy of the database is reinstated to a state of consistency and operational stability.
It should work, like you expect it, but may be you open another transaction within your class under test or you have an other feature/or bug somewhere.
Btw this annotations should be enougth:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:com/my/app/context.xml"} @Transactional public class PerformanceTest { @Test @Rollback(false) public void testMsisdnCreationPerformance() { // Create a JPA entity // Persist JPA entity } }
@See Spring Reference Chapter 9.3.5.4 Transaction management
Just add annotation Rollback and set the flag to false.
@Test @Rollback(false)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With