Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit tests always rollback the transactions

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?

like image 931
Juan Avatar asked Mar 22 '12 06:03

Juan


People also ask

Which of these annotations will you use for automatically rolling back a transaction after a test?

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.

Does @transactional rollback?

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.

What is transaction rollback?

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.

What is rollback testing?

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.


2 Answers

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

like image 186
Ralph Avatar answered Oct 16 '22 20:10

Ralph


Just add annotation Rollback and set the flag to false.

   @Test    @Rollback(false) 
like image 38
grep Avatar answered Oct 16 '22 18:10

grep