From d067547b1513e8124c5433dd1e00089c0e1ed012 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Wed, 24 Jul 2024 20:36:16 +0200 Subject: [PATCH] ExpectedExceptionToAssertThrows should not wrap single statement (#555) --- .../testing/junit5/ExpectedExceptionToAssertThrows.java | 7 +++++-- .../junit5/ExpectedExceptionToAssertThrowsTest.java | 5 ++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/openrewrite/java/testing/junit5/ExpectedExceptionToAssertThrows.java b/src/main/java/org/openrewrite/java/testing/junit5/ExpectedExceptionToAssertThrows.java index f4e66f5fb..ea09f6f06 100644 --- a/src/main/java/org/openrewrite/java/testing/junit5/ExpectedExceptionToAssertThrows.java +++ b/src/main/java/org/openrewrite/java/testing/junit5/ExpectedExceptionToAssertThrows.java @@ -160,8 +160,11 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration methodDecl Object expectedExceptionParam = (expectMethodInvocation == null || isExpectArgAMatcher) ? "Exception.class" : expectMethodInvocation.getArguments().get(0); - String templateString = expectedExceptionParam instanceof String ? "#{}assertThrows(#{}, () -> #{});" : "#{}assertThrows(#{any()}, () -> #{});"; + String templateString = expectedExceptionParam instanceof String ? "#{}assertThrows(#{}, () -> #{any()});" : "#{}assertThrows(#{any()}, () -> #{any()});"; + Statement statement = bodyWithoutExpectedExceptionCalls.getStatements().size() == 1 && + !(bodyWithoutExpectedExceptionCalls.getStatements().get(0) instanceof J.Throw) ? + bodyWithoutExpectedExceptionCalls.getStatements().get(0) : bodyWithoutExpectedExceptionCalls; m = JavaTemplate.builder(templateString) .contextSensitive() .javaParser(javaParser(ctx)) @@ -172,7 +175,7 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration methodDecl m.getCoordinates().replaceBody(), exceptionDeclParam, expectedExceptionParam, - bodyWithoutExpectedExceptionCalls + statement ); maybeAddImport("org.junit.jupiter.api.Assertions", "assertThrows"); diff --git a/src/test/java/org/openrewrite/java/testing/junit5/ExpectedExceptionToAssertThrowsTest.java b/src/test/java/org/openrewrite/java/testing/junit5/ExpectedExceptionToAssertThrowsTest.java index 0b61092b8..97503ff00 100644 --- a/src/test/java/org/openrewrite/java/testing/junit5/ExpectedExceptionToAssertThrowsTest.java +++ b/src/test/java/org/openrewrite/java/testing/junit5/ExpectedExceptionToAssertThrowsTest.java @@ -25,6 +25,7 @@ import static org.openrewrite.java.Assertions.java; +@SuppressWarnings({"deprecation", "JUnitMalformedDeclaration", "JUnit3StyleTestMethodInJUnit4Class", "Convert2MethodRef"}) class ExpectedExceptionToAssertThrowsTest implements RewriteTest { @Override @@ -105,9 +106,7 @@ class MyTest { @Test public void testEmptyPath() { - Throwable exception = assertThrows(IllegalArgumentException.class, () -> { - foo(); - }); + Throwable exception = assertThrows(IllegalArgumentException.class, () -> foo()); assertTrue(exception.getMessage().contains("Invalid location: gs://")); } void foo() {