Question

Why is the transaction being rolled back while there is no Exception? The object is not being saved. I have defined the transaction parameters like:

 @Transactional(value = "transactionManager", timeout = 30, rollbackFor =
 java.lang.Exception.class)`.  

This is what logs say about the transaction.

org.springframework.test.context.transaction.TransactionalTestExecutionListener
endTransaction
INFO: Rolled back transaction after test execution for test context [TestContext@51e2a069
testClass = DAOTest, testInstance = sample.library.dao.DAOTest@7591777e, testMethod =
testSaveDao@DAOTest, testException = [null], mergedContextConfiguration =
[MergedContextConfiguration@213c2d7f testClass = DAOTest, locations = 
'{classpath:/applicationcontext.xml}', classes = '{}', contextInitializerClasses = '[]',
activeProfiles = '{}', contextLoader = 
'org.springframework.test.context.support.DelegatingSmartContextLoader', parent =
[null]]]
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 3.634 sec

Here is the whole log of DAOTest class:

-------------------------------------------------------------------------------
Test set: sample.library.dao.DAOTest
-------------------------------------------------------------------------------
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 3.634 sec

what's being done wrong? I can give more information if necessary.

Was it helpful?

Solution

The INFO level should give a hint that this is not an error message. Your test rolls back by default, regardless of the primary setting on the service-level method. To avoid rollback (if you really want to) you need an annotation on the test class itself, as documented in the Annotations section of the Testing chapter in Spring documentation. The annotation in question is @Rollback.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top