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 9f0db001a..a3fd4ead3 100644 --- a/src/main/java/org/openrewrite/java/testing/junit5/RemoveTryCatchFailBlocks.java +++ b/src/main/java/org/openrewrite/java/testing/junit5/RemoveTryCatchFailBlocks.java @@ -63,7 +63,7 @@ private static class RemoveTryCatchBlocksFromUnitsTestsVisitor extends JavaVisit public J visitTry(J.Try jtry, ExecutionContext ctx) { J.Try try_ = (J.Try) super.visitTry(jtry, ctx); // only one catch block, such that we know it's safe to apply this recipe, and doesn't have resources - if (try_.getResources() != null || try_.getCatches().size() != 1) { + if (try_.getResources() != null || try_.getCatches().size() != 1 || try_.getFinally() != null) { return try_; } 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 6f6ec1dae..6cf2af614 100644 --- a/src/test/java/org/openrewrite/java/testing/junit5/RemoveTryCatchFailBlocksTest.java +++ b/src/test/java/org/openrewrite/java/testing/junit5/RemoveTryCatchFailBlocksTest.java @@ -18,6 +18,7 @@ import org.junit.jupiter.api.Test; import org.openrewrite.DocumentExample; import org.openrewrite.InMemoryExecutionContext; +import org.openrewrite.Issue; import org.openrewrite.java.JavaParser; import org.openrewrite.test.RecipeSpec; import org.openrewrite.test.RewriteTest; @@ -575,4 +576,31 @@ public void testMethod() { ) ); } + + @Test + @Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/489") + void doesNotRunonTryFinally() { + //language=java + rewriteRun( + java( + """ + import org.junit.jupiter.api.Assertions; + import org.junit.jupiter.api.Test; + + class MyTest { + @Test + public void testMethod() { + try { + int divide = 50 / 0; + } catch (ArithmeticException e) { + Assertions.fail(e.getMessage()); + } finally { + System.out.println("Some resource clean up should not be lost"); + } + } + } + """ + ) + ); + } }