-
Notifications
You must be signed in to change notification settings - Fork 72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace Mockito 1.x any(Class) and anyString() with nullable(Class) #320
Conversation
src/main/java/org/openrewrite/java/testing/mockito/AnyStringToNullable.java
Outdated
Show resolved
Hide resolved
…Nullable.java Co-authored-by: Tim te Beek <[email protected]>
Kun Li also reported a possible workaround; captured here in case we stick to ---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.testing.mockito.AnyToNullableApplicabilityCheck
displayName: todo
description: todo
tags:
- testing
- mockito
recipeList:
- org.openrewrite.maven.search.FindDependency:
groupId: org.mockito
artifactId: mockito-all
- org.openrewrite.gradle.search.FindDependency:
groupId: org.mockito
artifactId: mockito-all
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.testing.mockito.AnyToNullable
displayName: Replace Mockito 1.x `any(Class)` and `anyString()` with `nullable(Class)`
description: Since Mockito 2.10 `any(Class)` and `anyString()` no longer match null values. Use `nullable(Class)` instead.
tags:
- testing
- mockito
applicability:
anySource:
- org.openrewrite.java.testing.mockito.AnyToNullableApplicabilityCheck
recipeList:
- org.openrewrite.java.ChangeMethodName:
methodPattern: org.mockito.Mockito any(java.lang.Class)
newMethodName: nullable
- org.openrewrite.java.ChangeMethodTargetToStatic:
methodPattern: org.mockito.Mockito nullable(java.lang.Class)
fullyQualifiedTargetTypeName: org.mockito.ArgumentMatchers
- org.openrewrite.java.testing.mockito.AnyStringToNullable
--- |
As kindly suggested by @kunli2
I think this may still yield some missing cases as you're just checking to see if the dependency is there. Wouldn't it be better to check that the type or method was in use instead? That'd be a lot more exact and no longer be dependent upon GAV coordinates (since you could pull in the smaller mockito dependencies and desire this recipe to run for that case). |
Thanks for chiming in! Appreciate your concern to be exact. The issue here is there's a semantic change to the same methods On Still missing some cases with this PR as-is might then still be preferred over doing nothing, and strongly preferred over incorrectly changing anything when users are already on 2.10+. How would you feel we should proceed here given the above? |
So this one is going to be difficult for a few reasons.
The only way that I can think of to achieve this would be to use the |
Actually, just had an idea. Both Gradle and Maven have a Dependency Insight recipe. If we instead used this, which does look at the markers instead of the LST, then we can achieve our goal. So what I'm envisioning is something like this (1 programmatic recipes (already written), 3 declarative recipes (1 written, but needs modifications)):
For the DependencyInsight, we could do something like: This would grant us our exact desired outcome. WDYT? |
src/main/java/org/openrewrite/java/testing/mockito/AnyStringToNullable.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/java/testing/mockito/AnyStringToNullable.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/java/testing/mockito/AnyStringToNullable.java
Outdated
Show resolved
Hide resolved
src/test/java/org/openrewrite/java/testing/mockito/JunitMockitoUpgradeIntegrationTest.java
Outdated
Show resolved
Hide resolved
Thanks @timtebeek. Good ideas @shanman190 , would be happy to see those improvements made |
Fixes #319