Skip to content

MockitoWhenOnStaticToMockStatic: name unnamed _ variable when reusing MockedStatic#1060

Merged
timtebeek merged 4 commits into
mainfrom
tim/issue-1051
Jul 25, 2026
Merged

MockitoWhenOnStaticToMockStatic: name unnamed _ variable when reusing MockedStatic#1060
timtebeek merged 4 commits into
mainfrom
tim/issue-1051

Conversation

@timtebeek

@timtebeek timtebeek commented Jul 23, 2026

Copy link
Copy Markdown
Member

Fixes #1051. When a MockedStatic is held in an unnamed variable (try (var _ = Mockito.mockStatic(A.class))), the recipe emitted _.when(...), which does not compile; it now renames the resource to a generated name (e.g. mockA1) and uses that when reusing the existing MockedStatic, and renames multiple _ resources individually.

Blocked / draft: the added tests use unnamed _ variables, a Java 22+ feature that only parses with rewrite-java-25. This repo currently builds/tests on Java 21, so CI is red until the separate "migrate to build on Java 25" change lands on main; this PR should then be rebased (it carries no build changes of its own).

…ng MockedStatic

When a `MockedStatic` was held in an unnamed variable
(`try (var _ = Mockito.mockStatic(A.class))`), the recipe emitted
`_.when(...)`, which does not compile. Rename the resource to a generated
name and use that when reusing the existing `MockedStatic`.

Build tests on Java 25 with `rewrite-java-25` so unnamed `_` variables parse.
@timtebeek
timtebeek marked this pull request as draft July 23, 2026 17:02
@timtebeek

Copy link
Copy Markdown
Member Author

Needs a bump to Java 25 first, which we'll explore separately given that there's failures on main on Java 25.

@timtebeek
timtebeek marked this pull request as ready for review July 25, 2026 07:07
@timtebeek
timtebeek merged commit c9ebc4f into main Jul 25, 2026
1 check passed
@timtebeek
timtebeek deleted the tim/issue-1051 branch July 25, 2026 07:19
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Jul 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

MockitoWhenOnStaticToMockStatic fails when using unnamed var (_) notation

1 participant