Retain the original stubbing method when rewriting static when to MockedStatic - #1106
Merged
Conversation
timtebeek
force-pushed
the
tim/investigate-issue-1102
branch
from
August 25, 2026 15:01
a3da8d4 to
2b95017
Compare
…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
force-pushed
the
tim/investigate-issue-1102
branch
from
August 25, 2026 15:11
2b95017 to
8c272b9
Compare
timtebeek
marked this pull request as ready for review
August 25, 2026 15:23
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What's wrong
MockitoWhenOnStaticToMockStaticdecided whether to rewrite a statement by looking only at its select (Mockito.when(..)), never at the stubbing method being called on the resultingOngoingStubbing. All four rewrite templates — the three Java ones and the Kotlin one — then hardcoded.thenReturn(#{any()})and passedstatement.getArguments().get(0).So the outer call name was thrown away and replaced with
thenReturn: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: theAnswerlambda 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
thenReturnwith exactly one argument. Zero-argument stubbings (thenCallRealMethod()) are handled by treating the loneJ.Emptyargument as no arguments.Tests
Added coverage for
thenAnswer,thenThrow, multi-valuethenReturn,thenCallRealMethod, mixed stubbings sharing oneMockedStatic, a@BeforeEachvariant, and the Kotlin.use { }path.