Skip to content

Enhance RemoveInitMocksIfRunnersSpecified to also remove openMocks#904

Open
dsgrieve wants to merge 6 commits intomainfrom
dsgrieve/remove-openmocks-with-mockito-extension
Open

Enhance RemoveInitMocksIfRunnersSpecified to also remove openMocks#904
dsgrieve wants to merge 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

2 participants