Skip to content

Retain the original stubbing method when rewriting static when to MockedStatic - #1106

Merged
timtebeek merged 1 commit into
mainfrom
tim/investigate-issue-1102
Aug 25, 2026
Merged

Retain the original stubbing method when rewriting static when to MockedStatic#1106
timtebeek merged 1 commit into
mainfrom
tim/investigate-issue-1102

Conversation

@timtebeek

@timtebeek timtebeek commented Aug 25, 2026

Copy link
Copy Markdown
Member

What's wrong

MockitoWhenOnStaticToMockStatic decided whether to rewrite a statement by looking only at its select (Mockito.when(..)), never at the stubbing method being called on the resulting OngoingStubbing. All four rewrite templates — the three Java ones and the Kotlin one — then hardcoded .thenReturn(#{any()}) and passed statement.getArguments().get(0).

So the outer call name was thrown away and replaced with thenReturn:

Before After (wrong)
when(A.getNumber()).thenAnswer(inv -> -1) mockA.when(() -> A.getNumber()).thenReturn(inv -> -1)
when(A.getNumber()).thenThrow(new IllegalStateException()) mockA.when(() -> A.getNumber()).thenReturn(new IllegalStateException())
when(A.getNumber()).thenReturn(-1, -2, -3) mockA.when(() -> A.getNumber()).thenReturn(-1)
when(A.getNumber()).thenCallRealMethod() mockA.when(() -> A.getNumber()).thenReturn()

The reported case (thenAnswer) is the one that hurts most: the Answer lambda becomes the stubbed return value.

The fix

Carry the invoked method's simple name and its full argument list into the templates, instead of assuming thenReturn with exactly one argument. Zero-argument stubbings (thenCallRealMethod()) are handled by treating the lone J.Empty argument as no arguments.

Tests

Added coverage for thenAnswer, thenThrow, multi-value thenReturn, thenCallRealMethod, mixed stubbings sharing one MockedStatic, a @BeforeEach variant, and the Kotlin .use { } path.

…MockedStatic`

`MockitoWhenOnStaticToMockStatic` matched any `when(Static.method()).xxx(...)`
statement but always emitted `.thenReturn(firstArgument)`. That silently
turned `thenAnswer`/`thenThrow`/`then`/`thenCallRealMethod` into
`thenReturn`, and dropped all but the first argument of
`thenReturn(a, b, c)`.

Carry the invoked method name and its full argument list into the Java and
Kotlin templates instead.

Fixes #1102
@timtebeek
timtebeek force-pushed the tim/investigate-issue-1102 branch from 2b95017 to 8c272b9 Compare August 25, 2026 15:11
@timtebeek
timtebeek marked this pull request as ready for review August 25, 2026 15:23
@timtebeek
timtebeek merged commit a2f35dc into main Aug 25, 2026
1 check passed
@timtebeek
timtebeek deleted the tim/investigate-issue-1102 branch August 25, 2026 15:25
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Aug 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

org.openrewrite.java.testing.mockito.MockitoBestPractices Unnecessary operations bug Something isn't working

1 participant