Skip to content

Convert ArgumentMatcher anonymous class to lambda in Mockito migration#937

Merged
timtebeek merged 7 commits intomainfrom
bmuschko/mockito-matchers-recipe
Mar 24, 2026
Merged

Convert ArgumentMatcher anonymous class to lambda in Mockito migration#937
timtebeek merged 7 commits intomainfrom
bmuschko/mockito-matchers-recipe

Conversation

@bmuschko
Copy link
Contributor

@bmuschko bmuschko commented Mar 23, 2026

Summary

  • Add ArgumentMatcherMatchesParameterType recipe that converts anonymous ArgumentMatcher<T> implementations with matches(Object) to lambda expressions with the correct parameter type
  • Wire the new recipe into Mockito1to3Migration after the ChangeType from org.mockito.Matchers to org.mockito.ArgumentMatchers
  • Remove unnecessary casts of the parameter (e.g., (String) argument) within the method body

The Mockito1to3Migration recipe changes org.mockito.Matchers to
org.mockito.ArgumentMatchers but does not update the matches(Object)
method signature on ArgumentMatcher<T> implementations. This new
recipe converts anonymous ArgumentMatcher<T> implementations to
lambda expressions with the correct parameter type and removes
unnecessary casts.
@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite Mar 23, 2026
@bmuschko bmuschko added the enhancement New feature or request label Mar 23, 2026
Previously the recipe only converted anonymous classes where the
matches parameter was Object. Now it also handles cases where the
parameter is already correctly typed or where the type argument is
Object, consistently collapsing to a lambda in all cases.
Make the recipe class name more descriptive of what it actually does,
so it is clearer when used from a declarative recipe setup.
@github-project-automation github-project-automation bot moved this from In Progress to Ready to Review in OpenRewrite Mar 24, 2026
Use J.Parentheses.unwrap() instead of manually extracting and re-prefixing the identifier.
Copy link
Member

@timtebeek timtebeek left a comment

Choose a reason for hiding this comment

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

Thanks! Good to merge once CI passes.

@timtebeek timtebeek merged commit 9f7bb0e into main Mar 24, 2026
1 of 2 checks passed
@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite Mar 24, 2026
@timtebeek timtebeek deleted the bmuschko/mockito-matchers-recipe branch March 24, 2026 21:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

3 participants