From a13c52cf08f02bf0eec9478f4794c7eb04b011b0 Mon Sep 17 00:00:00 2001 From: Laurens Westerlaken Date: Thu, 8 Aug 2024 14:54:47 +0200 Subject: [PATCH] Solves https://github.com/openrewrite/rewrite-testing-frameworks/issues/547 --- .../junit5/RemoveTryCatchFailBlocks.java | 12 ++- .../junit5/RemoveTryCatchFailBlocksTest.java | 83 ++++++++++++------- 2 files changed, 61 insertions(+), 34 deletions(-) diff --git a/src/main/java/org/openrewrite/java/testing/junit5/RemoveTryCatchFailBlocks.java b/src/main/java/org/openrewrite/java/testing/junit5/RemoveTryCatchFailBlocks.java index 7e17762f6..404ba64a7 100644 --- a/src/main/java/org/openrewrite/java/testing/junit5/RemoveTryCatchFailBlocks.java +++ b/src/main/java/org/openrewrite/java/testing/junit5/RemoveTryCatchFailBlocks.java @@ -45,7 +45,7 @@ public String getDisplayName() { @Override public String getDescription() { return "Replace `try-catch` blocks where `catch` merely contains a `fail()` for `fail(String)` statement " + - "with `Assertions.assertDoesNotThrow(() -> { ... })`."; + "with `Assertions.assertDoesNotThrow(() -> { ... })`."; } @Override @@ -67,6 +67,10 @@ public J visitTry(J.Try jtry, ExecutionContext ctx) { return try_; } + if (try_.getBody().getStatements().stream().anyMatch((statement -> statement instanceof J.Return))) { + return try_; + } + /* Only one statement in the catch block, which is a fail(), with no or a simple String argument. We would not want to convert for instance fail(cleanUpAndReturnMessage()) might still have side @@ -82,8 +86,8 @@ We would not want to convert for instance fail(cleanUpAndReturnMessage()) might } J.MethodInvocation failCall = (J.MethodInvocation) statement; if (!ASSERT_FAIL_NO_ARG.matches(failCall) - && !ASSERT_FAIL_STRING_ARG.matches(failCall) - && !ASSERT_FAIL_THROWABLE_ARG.matches(failCall)) { + && !ASSERT_FAIL_STRING_ARG.matches(failCall) + && !ASSERT_FAIL_THROWABLE_ARG.matches(failCall)) { return try_; } @@ -150,4 +154,4 @@ private J.MethodInvocation replaceWithAssertDoesNotThrowWithStringExpression(Exe .apply(getCursor(), try_.getCoordinates().replace(), try_.getBody(), failCallArgument); } } -} +} \ No newline at end of file diff --git a/src/test/java/org/openrewrite/java/testing/junit5/RemoveTryCatchFailBlocksTest.java b/src/test/java/org/openrewrite/java/testing/junit5/RemoveTryCatchFailBlocksTest.java index 70afbcac4..1444e2ea3 100644 --- a/src/test/java/org/openrewrite/java/testing/junit5/RemoveTryCatchFailBlocksTest.java +++ b/src/test/java/org/openrewrite/java/testing/junit5/RemoveTryCatchFailBlocksTest.java @@ -45,7 +45,7 @@ void removeTryCatchBlock() { """ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; - + class MyTest { @Test public void testMethod() { @@ -60,7 +60,7 @@ public void testMethod() { """ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; - + class MyTest { @Test public void testMethod() { @@ -83,7 +83,7 @@ void removeStaticImportFail() { import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.fail; - + class MyTest { @Test public void testMethod() { @@ -98,7 +98,7 @@ public void testMethod() { """ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; - + class MyTest { @Test public void testMethod() { @@ -120,7 +120,7 @@ void removeTryCatchBlockWithoutMessage() { """ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; - + class MyTest { @Test public void testMethod() { @@ -135,7 +135,7 @@ public void testMethod() { """ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; - + class MyTest { @Test public void testMethod() { @@ -157,7 +157,7 @@ void removeTryCatchBlockWithFailString() { """ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; - + class MyTest { @Test public void testMethod() { @@ -172,7 +172,7 @@ public void testMethod() { """ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; - + class MyTest { @Test public void testMethod() { @@ -194,7 +194,7 @@ void failMethodArgIsNotGetMessage() { """ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Assertions; - + class MyTest { @Test void aTest() { @@ -204,7 +204,7 @@ void aTest() { Assertions.fail(cleanUpAndReturnMessage()); } } - + String cleanUpAndReturnMessage() { System.out.println("clean up code"); return "Oh no!"; @@ -223,7 +223,7 @@ void doesNotRunWithMultipleCatchBlocks() { """ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Assertions; - + class MyTest { @Test void aTest() { @@ -249,7 +249,7 @@ void catchHasMultipleStatements() { """ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Assertions; - + class MyTest { @Test void aTest() { @@ -275,7 +275,7 @@ void doesNotRunOnTryWithResources() { import org.junit.jupiter.api.Test; import java.io.PrintWriter; import org.junit.jupiter.api.Assertions; - + class MyTest { @Test void aTest() { @@ -299,7 +299,7 @@ void statementsBeforeAndAfterTryBlock() { """ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; - + class MyTest { @Test public void testMethod() { @@ -320,7 +320,7 @@ public void testMethod() { """ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; - + class MyTest { @Test public void testMethod() { @@ -348,7 +348,7 @@ void failWithStringThrowableArgs() { """ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; - + class MyTest { @Test void testMethod() { @@ -363,7 +363,7 @@ void testMethod() { """ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; - + class MyTest { @Test void testMethod() { @@ -386,7 +386,7 @@ void failWithSupplierStringAsIdentifier() { import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import java.util.function.Supplier; - + class MyTest { @Test void testMethod() { @@ -412,7 +412,7 @@ void failWithSupplierStringAsLambda() { import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import java.util.function.Supplier; - + class MyTest { @Test void testMethod() { @@ -436,7 +436,7 @@ void failWithThrowable() { """ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; - + class MyTest { @Test void testMethod() { @@ -451,7 +451,7 @@ void testMethod() { """ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; - + class MyTest { @Test void testMethod() { @@ -473,7 +473,7 @@ void multipleTryCatchBlocks() { """ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; - + class MyTest { @Test public void testMethod() { @@ -493,7 +493,7 @@ public void testMethod() { """ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; - + class MyTest { @Test public void testMethod() { @@ -518,7 +518,7 @@ void failHasBinaryWithException() { """ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; - + class MyTest { @Test public void testMethod() { @@ -533,7 +533,7 @@ public void testMethod() { """ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; - + class MyTest { @Test public void testMethod() { @@ -555,7 +555,7 @@ void failHasBinaryWithMethodCall() { """ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; - + class MyTest { @Test public void testMethod() { @@ -565,7 +565,7 @@ public void testMethod() { Assertions.fail("The error is: " + anotherMethod()); } } - + public String anotherMethod() { return "anotherMethod"; } @@ -602,7 +602,7 @@ public void testMethod() { import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import java.io.FileOutputStream; - + class MyTest { @Test public void testMethod() { @@ -625,7 +625,7 @@ void doesNotRunonTryFinally() { """ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; - + class MyTest { @Test public void testMethod() { @@ -642,4 +642,27 @@ public void testMethod() { ) ); } -} + + @Test + @Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/547") + void removeTryCatchFailBlocksWithReturningTry() { + rewriteRun( + spec -> spec.recipe(new RemoveTryCatchFailBlocks()), + java( + """ + package com.helloworld; + + import static org.junit.jupiter.api.Assertions.fail; + + public class Foo { + + String getFoo() { + try { + return "foo"; + } catch (RuntimeException e) { + fail(); + } + } + }""")); + } +} \ No newline at end of file