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 de757861d..add5ab8e7 100644 --- a/src/main/java/org/openrewrite/java/testing/junit5/AssertThrowsOnLastStatement.java +++ b/src/main/java/org/openrewrite/java/testing/junit5/AssertThrowsOnLastStatement.java @@ -56,8 +56,11 @@ public TreeVisitor getVisitor() { @Override public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration methodDecl, ExecutionContext ctx) { J.MethodDeclaration m = super.visitMethodDeclaration(methodDecl, ctx); - - m = m.withBody(m.getBody().withStatements(ListUtils.flatMap(m.getBody().getStatements(), methodStatement -> { + if (m.getBody() == null) { + return m; + } + doAfterVisit(new LambdaBlockToExpression().getVisitor()); + return m.withBody(m.getBody().withStatements(ListUtils.flatMap(m.getBody().getStatements(), methodStatement -> { J statementToCheck = methodStatement; final J.VariableDeclarations assertThrowsWithVarDec; final J.VariableDeclarations.NamedVariable assertThrowsVar; @@ -132,11 +135,7 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration methodDecl J.VariableDeclarations.NamedVariable newAssertThrowsVar = assertThrowsVar.withInitializer(newAssertThrows); return assertThrowsWithVarDec.withVariables(singletonList(newAssertThrowsVar)); }); - }))); - - doAfterVisit(new LambdaBlockToExpression().getVisitor()); - return m; } }); } 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 ad8cb779c..f91702b27 100644 --- a/src/test/java/org/openrewrite/java/testing/junit5/AssertThrowsOnLastStatementTest.java +++ b/src/test/java/org/openrewrite/java/testing/junit5/AssertThrowsOnLastStatementTest.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; @@ -252,4 +253,31 @@ void foo() { ) ); } + + @Test + @Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/618") + void bodyNull() { + //language=java + rewriteRun( + java( + """ + import org.junit.jupiter.api.Test; + + import static org.junit.jupiter.api.Assertions.*; + + class MyTest { + + @Test + void test() { + assertThrows(IllegalStateException.class, () -> System.out.println("foo")); + } + + interface InnerInterface { + String createParser(String input); + } + } + """ + ) + ); + } }