Issue
I am using Spring Boot 1.5.2.RELEASE and my service classes is annotated with
@Service
@Transactional(rollbackFor = Exception.class)
My unit test class is annotated with:
@RunWith(SpringRunner.class)
@SpringBootTest
I wish to roll back changes made in the database by unit test methods after test is finished.
One of the proposed solutions is to add @Transactional
annotation over the test class, I tried it but this solution produces some issues, is that sometimes the test transaction is rolled back (even without no exceptions thrown!) before the service transaction is completed.
Is there another good solution to rollback test?
Solution
The solution with the @Transactional
on the test class is the standard way of doing this. There is no way Spring Tx would roll back a transaction out of the blue, so I'd recommend looking closer into the issues that you encountered instead of inventing other solutions.
Answered By - Stanislav Bashkyrtsev
Answer Checked By - Senaida (JavaFixing Volunteer)