Enhance RemoveInitMocksIfRunnersSpecified to also remove openMocks#904
Open
Enhance RemoveInitMocksIfRunnersSpecified to also remove openMocks#904
Conversation
When `@ExtendWith(MockitoExtension.class)` or `@RunWith(MockitoJUnitRunner.class)` is present, this recipe now removes both: - `MockitoAnnotations.initMocks(this)` - `MockitoAnnotations.openMocks(this)` For the openMocks case, the recipe also handles: - Field declarations (e.g., `private AutoCloseable mocks`) - Assignment statements (e.g., `mocks = MockitoAnnotations.openMocks(this)`) - close() calls on the AutoCloseable field - Empty @AfterEach/@after methods after removing close() calls Closes moderneinc/customer-requests#1775
src/main/java/org/openrewrite/java/testing/mockito/RemoveInitMocksIfRunnersSpecified.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/java/testing/mockito/RemoveInitMocksIfRunnersSpecified.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/java/testing/mockito/RemoveInitMocksIfRunnersSpecified.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/java/testing/mockito/RemoveInitMocksIfRunnersSpecified.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/java/testing/mockito/RemoveInitMocksIfRunnersSpecified.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/java/testing/mockito/RemoveInitMocksIfRunnersSpecified.java
Show resolved
Hide resolved
src/main/java/org/openrewrite/java/testing/mockito/RemoveInitMocksIfRunnersSpecified.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/java/testing/mockito/RemoveInitMocksIfRunnersSpecified.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/java/testing/mockito/RemoveInitMocksIfRunnersSpecified.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/java/testing/mockito/RemoveInitMocksIfRunnersSpecified.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/java/testing/mockito/RemoveInitMocksIfRunnersSpecified.java
Show resolved
Hide resolved
src/main/java/org/openrewrite/java/testing/mockito/RemoveInitMocksIfRunnersSpecified.java
Show resolved
Hide resolved
src/main/java/org/openrewrite/java/testing/mockito/RemoveInitMocksIfRunnersSpecified.java
Show resolved
Hide resolved
src/main/java/org/openrewrite/java/testing/mockito/RemoveInitMocksIfRunnersSpecified.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/java/testing/mockito/RemoveInitMocksIfRunnersSpecified.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/java/testing/mockito/RemoveInitMocksIfRunnersSpecified.java
Outdated
Show resolved
Hide resolved
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
src/main/java/org/openrewrite/java/testing/mockito/RemoveInitMocksIfRunnersSpecified.java
Show resolved
Hide resolved
src/main/java/org/openrewrite/java/testing/mockito/RemoveInitMocksIfRunnersSpecified.java
Show resolved
Hide resolved
Jenson3210
reviewed
Feb 4, 2026
src/main/java/org/openrewrite/java/testing/mockito/RemoveInitMocksIfRunnersSpecified.java
Outdated
Show resolved
Hide resolved
Jenson3210
reviewed
Feb 4, 2026
Contributor
Jenson3210
left a comment
There was a problem hiding this comment.
Left 1 comment with "readability" question and updateCursor usage, but other than that this looks okay to me
src/main/java/org/openrewrite/java/testing/mockito/RemoveInitMocksIfRunnersSpecified.java
Show resolved
Hide resolved
src/main/java/org/openrewrite/java/testing/mockito/RemoveInitMocksIfRunnersSpecified.java
Show resolved
Hide resolved
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.
Summary
Extends
RemoveInitMocksIfRunnersSpecifiedrecipe to handleMockitoAnnotations.openMocks(this)in addition toinitMocksWhen
@ExtendWith(MockitoExtension.class)or@RunWith(MockitoJUnitRunner.class)is present, the recipe now removes the complete openMocks lifecycle:AutoCloseablefield declarationmocks = MockitoAnnotations.openMocks(this))close()call in@AfterEach/@AftermethodsCloses moderneinc/customer-requests#1775
Test plan
openMocks(this)simple caseopenMockswith static importopenMockswithout runners (no change expected)initMockstests still pass