-
Notifications
You must be signed in to change notification settings - Fork 79
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
Refactor Mockito.when
on static (non mock) to try with resource
#601
Conversation
src/test/java/org/openrewrite/java/testing/mockito/MockitoWhenOnStaticToMockStaticTest.java
Outdated
Show resolved
Hide resolved
I'm seeing failures with
Would you already want me to have a look? Or are you still exploring those? |
Also: could you hook this recipe into the Mockito migration already? That way we can't forget that step. |
String template = String.format("try(MockedStatic<%s> mock%s = mockStatic(%s.class)){\n" + | ||
"mock%s.when(%s::%s).thenReturn(%s);\n" + | ||
"}", arg_fq.getClassName(), arg_fq.getClassName(), arg_fq.getClassName(), arg_fq.getClassName(), arg_fq.getClassName(), ((J.MethodInvocation) when.getArguments().get(0)).getSimpleName(), ((J.MethodInvocation) stmt).getArguments().get(0)); | ||
m = JavaTemplate.builder(template) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of String.format
, can we use #{any()}
in the template as much as possible, and pass the matching LST elements as arguments into the JavaTemplate.apply? That way the type attribution is already correct on those elements.
src/main/java/org/openrewrite/java/testing/mockito/MockitoWhenOnStaticToMockStatic.java
Show resolved
Hide resolved
src/main/java/org/openrewrite/java/testing/mockito/MockitoWhenOnStaticToMockStatic.java
Outdated
Show resolved
Hide resolved
src/test/java/org/openrewrite/java/testing/mockito/MockitoWhenOnStaticToMockStaticTest.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/java/testing/mockito/MockitoWhenOnStaticToMockStatic.java
Outdated
Show resolved
Hide resolved
I'll have a look tomorrow, am aware of them :) |
Add missing imports Fill out name and desc
e690282
to
3d4b4d8
Compare
src/main/java/org/openrewrite/java/testing/mockito/MockitoWhenOnStaticToMockStatic.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/java/testing/mockito/MockitoWhenOnStaticToMockStatic.java
Outdated
Show resolved
Hide resolved
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've pushed up a couple changes already, mostly to adopt ListUtils instead of the binary before/after lists that were there previously.
Also: can you point me to any resources where the non-MockStatic usage was valid with older Mockito version? I'm having a hard time finding any sources on whether this recipe would actually match any cases in practices, as it seems to have been newly introduced in 3.4.0.
try (MockedStatic<com.foo.A> mockA = mockStatic(com.foo.A.class)) { | ||
mockA.when(A.getNumber()).thenReturn(-1); | ||
assertEquals(A.getNumber(), -1); | ||
|
||
try (MockedStatic<com.foo.A> mockA2 = mockStatic(com.foo.A.class)) { | ||
mockA2.when(A.getNumber()).thenReturn(-2); | ||
assertEquals(A.getNumber(), -2); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if this is the exact snippet we ought to be generating, but I do think we should account for more than one static mocked method.
src/main/java/org/openrewrite/java/testing/mockito/MockitoWhenOnStaticToMockStatic.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/java/testing/mockito/MockitoWhenOnStaticToMockStatic.java
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Managed to get the types shortened and added some meaningful names to the extracted method and arguments to drive home what we're after there. Thanks again for the work done here! Let's hope this helps.
What's your motivation?
Mockito.when
on static (non mock) is no longer allowed with Mockito 4Anything in particular you'd like reviewers to focus on?
Anyone you would like to review specifically?
@timtebeek
Checklist