Skip to content

Add recipe to replace MockitoTestExecutionListener#924

Merged
steve-aom-elliott merged 5 commits intomainfrom
replace-mockito-test-execution-listener
Mar 4, 2026
Merged

Add recipe to replace MockitoTestExecutionListener#924
steve-aom-elliott merged 5 commits intomainfrom
replace-mockito-test-execution-listener

Conversation

@steve-aom-elliott
Copy link
Contributor

@steve-aom-elliott steve-aom-elliott commented Mar 2, 2026

Summary

  • Adds ReplaceMockitoTestExecutionListener recipe that migrates away from @TestExecutionListeners(MockitoTestExecutionListener.class) to the appropriate Mockito initialization for the test framework in use
  • Supports JUnit 5 (@ExtendWith(MockitoExtension.class)), JUnit 4 (@RunWith(MockitoJUnitRunner.class) when no @RunWith exists), and TestNG (MockitoAnnotations.openMocks(this) lifecycle methods)
  • Registered in the Mockito1to3Migration composite recipe

TestNG approach

For TestNG classes (where JUnit's @ExtendWith and @RunWith are not available), the recipe adds MockitoAnnotations.openMocks(this) in a @BeforeMethod lifecycle method with the corresponding close() in @AfterMethod. This is a direct equivalent of what MockitoTestExecutionListener does internally:

  • MockitoTestExecutionListener calls openMocks() under the hood. Decompiling Spring Boot 3.x shows its initMocks() method calls MockitoAnnotations.openMocks(testContext.getTestInstance()) and stores the returned AutoCloseable for cleanup in afterTestMethod(). The recipe produces the same call sequence.

  • TestNG inherits @BeforeMethod/@AfterMethod from parent classes. When the recipe transforms a base class (e.g. BaseTest extends AbstractTestNGSpringContextTests), subclasses with @Mock fields inherit the lifecycle methods automatically. TestNG executes parent @BeforeMethod before child methods, and parent @AfterMethod after child methods.

  • openMocks(this) in a parent class initializes subclass @Mock fields. In Java, this always refers to the actual runtime instance (the subclass), so Mockito's reflection-based field scanning traverses the full class hierarchy and initializes all @Mock-annotated fields regardless of where they are declared.

Test plan

  • 14 unit tests covering JUnit 5, JUnit 4, TestNG, and negative cases
  • Full test suite passes
  • recipeCsvGenerate regenerated and validated

@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite Mar 2, 2026
@steve-aom-elliott steve-aom-elliott force-pushed the replace-mockito-test-execution-listener branch from 30f5ce0 to e2cd397 Compare March 2, 2026 17:53
…ropriate Mockito initialization

Adds ReplaceMockitoTestExecutionListener recipe that migrates away from
@TestExecutionListeners(MockitoTestExecutionListener.class) to the
appropriate Mockito initialization for the test framework in use:
- JUnit 5: @ExtendWith(MockitoExtension.class)
- JUnit 4: @RunWith(MockitoJUnitRunner.class) when no @RunWith exists
- TestNG: MockitoAnnotations.openMocks(this) lifecycle methods

Registered in the Mockito1to3Migration composite recipe.
- Update copyrights to 2026
- Skip TestNG migration when MockitoAnnotations.openMocks() is already used
- Place field at class top, initMocks before first method, closeMocks at end
- Regenerate recipes.csv
@steve-aom-elliott steve-aom-elliott force-pushed the replace-mockito-test-execution-listener branch from e2cd397 to 911e972 Compare March 2, 2026 19:39
…nMocks

- Replace manual isTestNGType recursion with TypeUtils.isAssignableTo
  for AbstractTestNGSpringContextTests, properly handling full hierarchy
- When openMocks is already present but close() is missing, add
  @AfterMethod closeMocks and still remove @TestExecutionListeners
- When both openMocks and @AfterMethod exist, only remove the annotation
@steve-aom-elliott steve-aom-elliott moved this from In Progress to Ready to Review in OpenRewrite Mar 2, 2026
Replace @Getter with @Value/@EqualsAndHashCode on the recipe class.
Replace hasOpenMocksCall() with MethodAccess.Matcher and
hasAfterMethodAnnotation() with Annotated.Matcher trait patterns.
}

// Check compilation unit imports
J.CompilationUnit cu = getCursor().firstEnclosingOrThrow(J.CompilationUnit.class);
Copy link
Collaborator

Choose a reason for hiding this comment

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

We could use FindImports here I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah just saw these. I'll make another PR quickly


// Check compilation unit imports
J.CompilationUnit cu = getCursor().firstEnclosingOrThrow(J.CompilationUnit.class);
for (J.Import imp : cu.getImports()) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we could use 2-3 FindImport here

@steve-aom-elliott steve-aom-elliott merged commit 5c07d95 into main Mar 4, 2026
1 check passed
@steve-aom-elliott steve-aom-elliott deleted the replace-mockito-test-execution-listener branch March 4, 2026 15:07
@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite Mar 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants