The Unit-Test Classes I have in my Project extend an AbstractUnitTest
class which is @Transactional
. The tests in these classes are running on some methods which themselves are @Transactional
.
Whenever these methods bring tested fail, instead of rolling back, the Transactions are partially persisted ( in the Testing Environment, ordinarily they behave atomically ). This is preventing me from testing Transactional behavior of my Project Modules.
More specifically , I have a Unit-Test for an addList()
method. However, on encountering an Exception, during unit-testing, the input list is partially added and not rolled back since , the test is itself running in a Transaction(by virtue of AbstractUnitTest
being Transactional)
On removing the @Transactional
from AbstractUnitTest
, the tests work fine. What is the reason for this behavior and how can you perform unit-testing of Transactional methods if the Unit Test class itself is Transactional?
that depends on your configuration I think: if you want to start a transaction you need to configure a TransactionManager for your unit testing. As you use Spring make sure this is configured and the transaction boundaries (begin/commit or rollback) are defined to ensure the transactional behavior (rollback upon error).
My advice is anyway to use TransactionalTestExecutionListener to always rollback after each test: in this way each test is independent as it always runs with the same data.
Hope this helps.
Beppe
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.