Skip to content

Commit

Permalink
Skip interface methods without a body in AssertThrowsOnLastStatement
Browse files Browse the repository at this point in the history
Fixes #618
  • Loading branch information
timtebeek committed Oct 21, 2024
1 parent 8fb11c8 commit aa80a5c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ public TreeVisitor<?, ExecutionContext> 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;
Expand Down Expand Up @@ -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;
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
"""
)
);
}
}

0 comments on commit aa80a5c

Please sign in to comment.