Skip to content
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

Merged

Conversation

klauerm
Copy link
Contributor

@klauerm klauerm commented Feb 15, 2023

No description provided.

klauerm and others added 30 commits January 27, 2023 15:07
…rMockitoIntegrationTest.java

Co-authored-by: Tim te Beek <timtebeek@gmail.com>
- First it removes the PowerMockTestCaseConfig extension
…template which I think is a bug in OpenRewrite.
- 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
@klauerm
Copy link
Contributor Author

klauerm commented Feb 22, 2023

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.
@sambsnyd @timtebeek @jkschneider

Copy link
Contributor

@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.

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.

@timtebeek
Copy link
Contributor

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!

@timtebeek timtebeek marked this pull request as ready for review February 23, 2023 08:18
Copy link
Member

@sambsnyd sambsnyd left a 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.

Comment on lines 369 to 382
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()));
Copy link
Member

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.

Comment on lines +424 to +431
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(),
Copy link
Member

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.

Comment on lines 165 to 170
JavaTemplate.builder(() -> getCursor().getParentTreeCursor(),
"mocked#{any(org.mockito.MockedStatic)} = #{any(org.mockito.Mockito)};")
.javaParser(() -> JavaParser.fromJavaVersion()
.classpathFromResources(ctx, "mockito-core-3.*")
.build())
.build(),
Copy link
Member

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.

@klauerm
Copy link
Contributor Author

klauerm commented Feb 24, 2023

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.

@klauerm klauerm marked this pull request as draft February 24, 2023 09:40
@klauerm klauerm marked this pull request as ready for review March 11, 2023 10:52
@timtebeek
Copy link
Contributor

Thank you for your continued work on this @klauerm !

@klauerm
Copy link
Contributor Author

klauerm commented Mar 13, 2023

I'm done with the implementation. Please review and merge, if there are no issues.

@timtebeek timtebeek requested a review from sambsnyd March 13, 2023 12:41
@sambsnyd sambsnyd merged commit d4e0b9d into openrewrite:main Apr 7, 2023
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
recipe Recipe request
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

4 participants