RemoveInitMocksIfRunnersSpecified: handle openMocks(this) as a try-with-resources resource - #1118
Draft
kmccarp wants to merge 1 commit into
Draft
RemoveInitMocksIfRunnersSpecified: handle openMocks(this) as a try-with-resources resource#1118kmccarp wants to merge 1 commit into
kmccarp wants to merge 1 commit into
Conversation
…th-resources resource The recipe dropped the initializer of a try-with-resources variable when it was MockitoAnnotations.openMocks(this) or initMocks(this), leaving 'try (AutoCloseable mocks;)' which does not compile. Remove the resource instead, and unwrap the try into its body when nothing else remains. Keep a plain block when inlining would collide with a later local or lose comments, leave a try alone when its variable is still referenced in the body or when the try is not directly inside a block, and never drop a call that is a variable initializer.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Background / problem
RemoveInitMocksIfRunnersSpecifiedremovesMockitoAnnotations.initMocks(this)andopenMocks(this)once a test class is on@ExtendWith(MockitoExtension.class)or@RunWith(MockitoJUnitRunner.class). It only understood the call as a statement or as the right-hand side of an assignment. A user's test suite wrote it as a try-with-resources variable,try (AutoCloseable mocks = MockitoAnnotations.openMocks(this)) { ... }, and the recipe stripped the initializer, leavingtry (AutoCloseable mocks;) {, which does not compile. Every affected test had to be fixed by hand.Solution
The recipe now removes the resource itself and, when the
tryhas nothing left, replaces it with its body so the test reads as if the call had never been there. It keeps the body as a plain block when inlining it would change meaning, and it leaves thetryuntouched in the few cases where there is no safe rewrite, so the output always compiles.Test plan
RemoveInitMocksIfRunnersSpecifiedTestfor the reported input, a second resource alongside it, and the cases where the recipe must fall back or leave the code alonemainand pass with the changeorg.openrewrite.java.testing.mockito.*andorg.openrewrite.java.testing.junit5.*pass