Skip to content

Commit

Permalink
Call super.visitMethodInvocation first, as is customary unless explic…
Browse files Browse the repository at this point in the history
…itly needed in a different order
  • Loading branch information
timtebeek committed Nov 5, 2024
1 parent e31cd2d commit 6452e0f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {

@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
List<Expression> args = method.getArguments();
J.MethodInvocation md = super.visitMethodInvocation(method, ctx);

List<Expression> args = md.getArguments();
if (args.size() < 2 || args.size() > 3) {
return super.visitMethodInvocation(method, ctx);
return md;
}

J.MethodInvocation md;
Expression expectedType = args.get(0);
Expression actualValue = args.get(1);

Expand All @@ -74,7 +74,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
maybeAddImport("org.assertj.core.api.Assertions", "assertThat", false);
maybeRemoveImport("org.junit.jupiter.api.Assertions");

return super.visitMethodInvocation(md, ctx);
return md;
}

private J.MethodInvocation rewriteAssertToInstance(JavaTemplate.Builder template, J.MethodInvocation method, ExecutionContext ctx, Object... parameters) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,32 @@ void test() {
);
}

@Test
void doesConvertNestedMethodInvocations() {
rewriteRun(
// language=java
java(
"""
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertAll;
class Test {
void test() {
assertAll(() -> assertInstanceOf(Integer.class, 4));
}
}
""",
"""
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;
class Test {
void test() {
assertAll(() -> assertThat(4).isInstanceOf(Integer.class));
}
}
"""
)
);
}
}

0 comments on commit 6452e0f

Please sign in to comment.