-
Notifications
You must be signed in to change notification settings - Fork 52
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
Further improvements to replace powermockito method calls by mockito #315
Further improvements to replace powermockito method calls by mockito #315
Conversation
…rMockitoIntegrationTest.java Co-authored-by: Tim te Beek <timtebeek@gmail.com>
…rMockitoIntegrationTest.java
- First it removes the PowerMockTestCaseConfig extension
…template which I think is a bug in OpenRewrite.
…commit of code review
- Unit tests are failing because of missing type
# Conflicts: # build.gradle.kts # src/main/java/org/openrewrite/java/testing/mockito/PowerMockitoMockStaticToMockito.java # src/test/java/org/openrewrite/java/testing/mockito/PowerMockitoMockStaticToMockitoTest.java
- Unit tests are failing because of missing type
- Unit tests are failing because of missing type
- The testNG case is still missing - Unit tests are failing because of missing type
Move static mock creation into before method
…sspathFromResources
The rewrite on our code base is working now. Can someone please help me to fix the failing unit tests in ReplacePowerMockitoIntegrationTest. That would be great. |
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.
We've discussed the option within the team to have the tests pass, for now, by ignoring the type errors within the recipe. The type issues are nested far down, so unlikely to trip up subsequent recipe executions. If they do, we can revisit the issue by adding the type attribution as required.
src/test/java/org/openrewrite/java/testing/mockito/ReplacePowerMockitoIntegrationTest.java
Show resolved
Hide resolved
src/test/java/org/openrewrite/java/testing/mockito/ReplacePowerMockitoIntegrationTest.java
Show resolved
Hide resolved
Just wanted to say, again, we really appreciate the amount of work you've put into this! I know it's been a tough one to see through in full, but we're glad to hear on your end you've already been able to migrate successfully. Thanks again! |
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.
Sorry it took so long for me to review this. I left some suggestions which may help. Any time there is missing type attribution after running a recipe which uses JavaTemplate
missing imports on JavaTemplate.Builder
is the first thing I check for. In any situation where you're not completely sure that an import will always already be present for every type referenced within the template go ahead and add one.
src/main/java/org/openrewrite/java/testing/mockito/PowerMockitoMockStaticToMockito.java
Outdated
Show resolved
Hide resolved
List<J.Block> methodBodies = classBody.getStatements().stream() | ||
.filter(statement -> statement instanceof J.MethodDeclaration) | ||
.map(J.MethodDeclaration.class::cast) | ||
.map(J.MethodDeclaration::getBody) | ||
.filter(Objects::nonNull) | ||
.collect(Collectors.toList()); | ||
Set<J.MethodInvocation> mockStaticMethodInvocations = new HashSet<>(); | ||
for (J.Block methodBody : methodBodies) { | ||
mockStaticMethodInvocations.addAll( | ||
methodBody.getStatements() | ||
.stream().filter(statement -> statement instanceof J.MethodInvocation) | ||
.map(J.MethodInvocation.class::cast) | ||
.filter(MOCKED_STATIC_CLOSE_MATCHER::matches) | ||
.filter(methodInvocation -> methodInvocation.getSelect() instanceof J.Identifier) | ||
.anyMatch(methodInvocation -> ((J.Identifier) methodInvocation.getSelect()).getSimpleName() | ||
.equals(staticMock.getSimpleName())); | ||
.filter(MOCKED_STATIC_MATCHER::matches) | ||
.collect(Collectors.toSet())); |
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.
Iterating over the statements in a method body will miss any statements nested inside of another statement, such as a loop or conditional.
void foo() {
bar(); // will be found iterating over statements
if(true) {
bar(); // will not be found iterating over statements
}
}
Dealing with the nested nature of these hierarchical structures is exactly what the visitor pattern is for. Consider newing up a JavaVisitor
inline to collect method invocations.
JavaTemplate.builder(() -> getCursor().getParentTreeCursor(), | ||
"private MockedStatic<#{}> " + MOCK_PREFIX + "#{};") | ||
.javaParser(() -> JavaParser.fromJavaVersion() | ||
.classpathFromResources(ctx, "mockito-core-3.12.4") | ||
.build()) | ||
.staticImports("org.mockito.Mockito.mockStatic") | ||
.imports(MOCKED_STATIC) | ||
.build(), |
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.
This may be missing a calls JavaTemplate.Builder.import()
for MockedStatic
and fullyQualifiedType
. Possible source of missing type attribution as surfaced in the test cases.
src/main/java/org/openrewrite/java/testing/mockito/PowerMockitoMockStaticToMockito.java
Outdated
Show resolved
Hide resolved
JavaTemplate.builder(() -> getCursor().getParentTreeCursor(), | ||
"mocked#{any(org.mockito.MockedStatic)} = #{any(org.mockito.Mockito)};") | ||
.javaParser(() -> JavaParser.fromJavaVersion() | ||
.classpathFromResources(ctx, "mockito-core-3.*") | ||
.build()) | ||
.build(), |
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.
Possibly missing imports for MockedStatic
and Mockito
. I'm not 100% sure from code review alone if it makes a difference in this context, but since the tests are showing some missing type attribution, I'll point this out as a possible source.
Thank you for the comments. I will look into this. Please do not merge this PR as it is still a draft and I need to implement at least 2 more features that popped up as issues during my testing. |
src/test/java/org/openrewrite/java/testing/mockito/ReplacePowerMockitoIntegrationTest.java
Outdated
Show resolved
Hide resolved
src/test/java/org/openrewrite/java/testing/mockito/ReplacePowerMockitoIntegrationTest.java
Outdated
Show resolved
Hide resolved
src/test/java/org/openrewrite/java/testing/mockito/ReplacePowerMockitoIntegrationTest.java
Outdated
Show resolved
Hide resolved
# Conflicts: # src/test/java/org/openrewrite/java/testing/mockito/PowerMockitoMockStaticToMockitoTest.java # src/test/java/org/openrewrite/java/testing/mockito/ReplacePowerMockitoIntegrationTest.java
Thank you for your continued work on this @klauerm ! |
I'm done with the implementation. Please review and merge, if there are no issues. |
No description provided.