From 6452e0fed7ad1a87d51fb285ef56d96ca336a45a Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Tue, 5 Nov 2024 09:46:11 +0100 Subject: [PATCH] Call super.visitMethodInvocation first, as is customary unless explicitly needed in a different order --- .../JUnitAssertInstanceOfToAssertThat.java | 8 +++--- ...JUnitAssertInstanceOfToAssertThatTest.java | 28 +++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/openrewrite/java/testing/assertj/JUnitAssertInstanceOfToAssertThat.java b/src/main/java/org/openrewrite/java/testing/assertj/JUnitAssertInstanceOfToAssertThat.java index 7c03f3764..e08d2f1fe 100644 --- a/src/main/java/org/openrewrite/java/testing/assertj/JUnitAssertInstanceOfToAssertThat.java +++ b/src/main/java/org/openrewrite/java/testing/assertj/JUnitAssertInstanceOfToAssertThat.java @@ -48,13 +48,13 @@ public TreeVisitor getVisitor() { @Override public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { - List args = method.getArguments(); + J.MethodInvocation md = super.visitMethodInvocation(method, ctx); + List 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); @@ -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) { diff --git a/src/test/java/org/openrewrite/java/testing/assertj/JUnitAssertInstanceOfToAssertThatTest.java b/src/test/java/org/openrewrite/java/testing/assertj/JUnitAssertInstanceOfToAssertThatTest.java index 66cdfa55d..10579e3c2 100644 --- a/src/test/java/org/openrewrite/java/testing/assertj/JUnitAssertInstanceOfToAssertThatTest.java +++ b/src/test/java/org/openrewrite/java/testing/assertj/JUnitAssertInstanceOfToAssertThatTest.java @@ -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)); + } + } + """ + ) + ); + } }