Knowledge Transfer

Ethickfox kb page with all notes


Project maintained by ethickfox Hosted on GitHub Pages — Theme by mattgraham

Put @Transactional on your test class or methods and test-managed transactions will automatically be rollbacked.

Since it's a test, the transaction should be by default rolledback thanks to TransactionalTestExecutionListener. Now you are asking why something is committed anyway, it can be because of inner transactions. @Transactional spans the transaction for entire test method, so if you use some dao (like in your case) that transaction will be rolledback also, however if some method uses transaction propagation type other than REQUIRED, for example REQUIRED_NEW, call to db can be performed anyway, because REQUIRED_NEW suspends current transaction from the test and creates a new one. That new one will be committed. There is also flush time issue, due hibernate caching - he makes a call to db at the end of transaction, usually at the end of the method, so fetching elements directly after persisting them may fail. Both things can be tricky.

https://relentlesscoding.com/posts/automatic-rollback-of-transactions-in-spring-tests/

Is it a good practice to use @Transactional on test methods?

Sure, but not to test transaction boundaries. Remember that you can use also @DataJpaTest, which places @Transactional for every test method.

7.png

Untitled 1 43.png

2.png

3.png