Skip to content

Add JUnit 4 support to AddMockitoExtensionIfAnnotationsUsed#922

Merged
bmuschko merged 1 commit intomainfrom
bmuschko/fix-mockito-spy-cast
Feb 28, 2026
Merged

Add JUnit 4 support to AddMockitoExtensionIfAnnotationsUsed#922
bmuschko merged 1 commit intomainfrom
bmuschko/fix-mockito-spy-cast

Conversation

@bmuschko
Copy link
Contributor

@bmuschko bmuschko commented Feb 28, 2026

Summary

  • Extends AddMockitoExtensionIfAnnotationsUsed to handle JUnit 4 tests by adding @RunWith(MockitoJUnitRunner.class) when Mockito annotations (@Mock, @Captor, @Spy, @InjectMocks) are present

  • Fixes the issue where PowerMock removal leaves JUnit 4 @Mock fields uninitialized because no runner replacement is added

  • Skips adding @RunWith when any @RunWith annotation is already present (e.g., SpringRunner, JUnit4)

  • Closes https://github.com/moderneinc/customer-requests/issues/1932

When PowerMock's @RunWith(PowerMockRunner.class) is removed from JUnit 4
tests, mock fields annotated with @mock are never initialized. This adds
@RunWith(MockitoJUnitRunner.class) for JUnit 4 tests using Mockito
annotations, complementing the existing JUnit 5 @ExtendWith support.
@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite Feb 28, 2026
@bmuschko bmuschko added the bug Something isn't working label Feb 28, 2026
@github-project-automation github-project-automation bot moved this from In Progress to Ready to Review in OpenRewrite Feb 28, 2026
@bmuschko bmuschko merged commit 63820f4 into main Feb 28, 2026
1 check passed
@bmuschko bmuschko deleted the bmuschko/fix-mockito-spy-cast branch February 28, 2026 16:01
@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite Feb 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants