diff --git a/src/main/java/org/openrewrite/java/testing/junit5/AssertThrowsOnLastStatement.java b/src/main/java/org/openrewrite/java/testing/junit5/AssertThrowsOnLastStatement.java index ffbb0f330..de757861d 100644 --- a/src/main/java/org/openrewrite/java/testing/junit5/AssertThrowsOnLastStatement.java +++ b/src/main/java/org/openrewrite/java/testing/junit5/AssertThrowsOnLastStatement.java @@ -38,13 +38,14 @@ public class AssertThrowsOnLastStatement extends Recipe { @Override public String getDisplayName() { - return "Applies Junit 5 assertThrows on last statement in lamdba block only"; + return "Applies JUnit 5 `assertThrows` on last statement in lambda block only"; } @Override public String getDescription() { - return "Applies Junit 5 assertThrows on last statement in lambda block only, in extremely rare cases may cause " + - "compilation errors if lambda uses non final variables."; + return "Applies JUnit 5 `assertThrows` on last statement in lambda block only. " + + "In rare cases may cause compilation errors if the lambda uses effectively non final variables. " + + "In some cases, tests might fail if earlier statements in the lambda block throw exceptions."; } @Override @@ -101,10 +102,6 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration methodDecl } J.Block body = (J.Block) lambda.getBody(); - if (body == null) { - return methodStatement; - } - List lambdaStatements = body.getStatements(); if (lambdaStatements.size() <= 1) { return methodStatement; diff --git a/src/main/resources/META-INF/rewrite/junit5.yml b/src/main/resources/META-INF/rewrite/junit5.yml index 54f2ca7ab..a8a38c9cf 100755 --- a/src/main/resources/META-INF/rewrite/junit5.yml +++ b/src/main/resources/META-INF/rewrite/junit5.yml @@ -32,6 +32,7 @@ recipeList: - org.openrewrite.java.testing.junit5.RemoveDuplicateTestTemplates - org.openrewrite.java.testing.junit5.RemoveTryCatchFailBlocks - org.openrewrite.java.testing.junit5.LifecycleNonPrivate + - org.openrewrite.java.testing.junit5.AssertThrowsOnLastStatement --- type: specs.openrewrite.org/v1beta/recipe name: org.openrewrite.java.testing.junit5.StaticImports diff --git a/src/test/java/org/openrewrite/java/testing/junit5/AssertThrowsOnLastStatementTest.java b/src/test/java/org/openrewrite/java/testing/junit5/AssertThrowsOnLastStatementTest.java index 744fed420..ad8cb779c 100644 --- a/src/test/java/org/openrewrite/java/testing/junit5/AssertThrowsOnLastStatementTest.java +++ b/src/test/java/org/openrewrite/java/testing/junit5/AssertThrowsOnLastStatementTest.java @@ -43,12 +43,12 @@ void applyToLastStatementWithDeclaringVariableThreeLines() { java( """ import org.junit.jupiter.api.Test; - + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; - + class MyTest { - + @Test public void test() { Throwable exception = assertThrows(IllegalArgumentException.class, () -> { @@ -64,12 +64,12 @@ void foo() { """, """ import org.junit.jupiter.api.Test; - + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; - + class MyTest { - + @Test public void test() { foo(); @@ -93,12 +93,12 @@ void applyToLastStatementWithDeclaringVariableThreeLinesHasLineBefore() { java( """ import org.junit.jupiter.api.Test; - + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; - + class MyTest { - + @Test public void test() { System.out.println("bla"); @@ -115,12 +115,12 @@ void foo() { """, """ import org.junit.jupiter.api.Test; - + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; - + class MyTest { - + @Test public void test() { System.out.println("bla"); @@ -145,11 +145,11 @@ void applyToLastStatementNoDeclaringVariableTwoLinesNoLinesAfter() { java( """ import org.junit.jupiter.api.Test; - + import static org.junit.jupiter.api.Assertions.assertThrows; - + class MyTest { - + @Test public void test() { assertThrows(IllegalArgumentException.class, () -> { @@ -163,11 +163,11 @@ void foo() { """, """ import org.junit.jupiter.api.Test; - + import static org.junit.jupiter.api.Assertions.assertThrows; - + class MyTest { - + @Test public void test() { System.out.println("foo"); @@ -189,11 +189,11 @@ void applyToLastStatementHasMessage() { java( """ import org.junit.jupiter.api.Test; - + import static org.junit.jupiter.api.Assertions.assertThrows; - + class MyTest { - + @Test public void test() { assertThrows(IllegalArgumentException.class, () -> { @@ -207,11 +207,11 @@ void foo() { """, """ import org.junit.jupiter.api.Test; - + import static org.junit.jupiter.api.Assertions.assertThrows; - + class MyTest { - + @Test public void test() { System.out.println("foo"); @@ -234,12 +234,12 @@ void makeNoChangesAsOneLine() { java( """ import org.junit.jupiter.api.Test; - + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; - + class MyTest { - + @Test public void test() { Throwable exception = assertThrows(IllegalArgumentException.class, () -> foo());