Skip to content

Enhance RemoveInitMocksIfRunnersSpecified to also remove openMocks#904

Merged
Jenson3210 merged 6 commits intomainfrom
dsgrieve/remove-openmocks-with-mockito-extension
Feb 6, 2026
Merged

Enhance RemoveInitMocksIfRunnersSpecified to also remove openMocks#904
Jenson3210 merged 6 commits intomainfrom
dsgrieve/remove-openmocks-with-mockito-extension

Conversation

@dsgrieve
Copy link
Contributor

@dsgrieve dsgrieve commented Jan 29, 2026

Summary

  • Extends RemoveInitMocksIfRunnersSpecified recipe to handle MockitoAnnotations.openMocks(this) in addition to initMocks

  • When @ExtendWith(MockitoExtension.class) or @RunWith(MockitoJUnitRunner.class) is present, the recipe now removes the complete openMocks lifecycle:

    • The AutoCloseable field declaration
    • The assignment statement (mocks = MockitoAnnotations.openMocks(this))
    • The close() call in @AfterEach/@After methods
    • Empty setup/teardown methods after removal
  • Closes moderneinc/customer-requests#1775

Test plan

  • Added test for removing openMocks(this) simple case
  • Added test for removing openMocks with static import
  • Added test for NOT removing openMocks without runners (no change expected)
  • Added test for complete openMocks lifecycle (field + assignment + close() + AfterEach)
  • Verified existing initMocks tests still pass

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
@dsgrieve dsgrieve requested a review from timtebeek January 30, 2026 19:16
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Copy link
Contributor

@Jenson3210 Jenson3210 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left 1 comment with "readability" question and updateCursor usage, but other than that this looks okay to me

@dsgrieve dsgrieve requested a review from Jenson3210 February 5, 2026 16:30
@github-project-automation github-project-automation bot moved this from In Progress to Ready to Review in OpenRewrite Feb 6, 2026
@Jenson3210 Jenson3210 merged commit cf78c5a into main Feb 6, 2026
2 checks passed
@Jenson3210 Jenson3210 deleted the dsgrieve/remove-openmocks-with-mockito-extension branch February 6, 2026 11:23
@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite Feb 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants