Fix @TestTransaction for self-intercepted invocation #16420
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This caused quite some confusion in my team as all of our test classes using
@TestTransaction
were working just fine, but for a single one we were getting something like that (this is actually fromTestTransactionTest
after I extended it):which is very confusing because on rollback it has no tx anymore?!
Turns out that by blindly calling rollback(), the
TestTransactionInterceptor
was shadowing the actual problem:which in turn revealed that the interceptor does not consider self-intercepted invocations of non-private non-test methods from test methods. It was trying to begin a new transaction despite having already done so for the "outer" invocation (the actual test method).
In our case that public helper method was actually an oversight and it should have been private instead. But I don't see why this should be forbidden.
Of course, all this only happens if you use
@TestTransaction
for the entire test class but IMO that is a good DRY-approach.TL;DR, this PR:
begin()
attempts