From a5f65cecb3ee678ba6131d2e698c74061c4e7b89 Mon Sep 17 00:00:00 2001 From: Laurens Westerlaken Date: Thu, 8 Aug 2024 16:15:53 +0200 Subject: [PATCH] Add testcase as provided in https://github.com/openrewrite/rewrite-testing-frameworks/issues/511 --- ...AssertThrowsToAssertExceptionTypeTest.java | 50 ++++++++++++++++--- 1 file changed, 44 insertions(+), 6 deletions(-) diff --git a/src/test/java/org/openrewrite/java/testing/assertj/JUnitAssertThrowsToAssertExceptionTypeTest.java b/src/test/java/org/openrewrite/java/testing/assertj/JUnitAssertThrowsToAssertExceptionTypeTest.java index d9651278b..5d57c99a9 100644 --- a/src/test/java/org/openrewrite/java/testing/assertj/JUnitAssertThrowsToAssertExceptionTypeTest.java +++ b/src/test/java/org/openrewrite/java/testing/assertj/JUnitAssertThrowsToAssertExceptionTypeTest.java @@ -76,9 +76,9 @@ void memberReference() { import static org.junit.jupiter.api.Assertions.assertThrows; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; - + public class MemberReferenceTest { - + public void throwsWithMemberReference() { CompletableFuture future = new CompletableFuture<>(); assertThrows(ExecutionException.class, future::get); @@ -89,9 +89,9 @@ public void throwsWithMemberReference() { import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; - + public class MemberReferenceTest { - + public void throwsWithMemberReference() { CompletableFuture future = new CompletableFuture<>(); assertThatExceptionOfType(ExecutionException.class).isThrownBy(future::get); @@ -110,7 +110,7 @@ void assertThrowsAssignment() { java( """ import static org.junit.jupiter.api.Assertions.assertThrows; - + public class SimpleExpectedExceptionTest { public void throwsExceptionWithSpecificType() { NullPointerException npe = assertThrows(NullPointerException.class, () -> { @@ -135,7 +135,7 @@ void assertThrowsTernaryAssignment() { java( """ import static org.junit.jupiter.api.Assertions.assertThrows; - + public class SimpleExpectedExceptionTest { public void throwsExceptionWithSpecificType() { NullPointerException npe = hashCode() == 42 @@ -149,4 +149,42 @@ public void throwsExceptionWithSpecificType() { ) ); } + + @Test + @Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/511") + void assertThrowsExecutableVariable() { + //language=java + rewriteRun( + java( + """ + import org.junit.jupiter.api.function.Executable; + + import static org.junit.jupiter.api.Assertions.assertThrows; + + public class SimpleExpectedExceptionTest { + public void throwsExceptionWithSpecificType() { + Executable executable = () -> { + throw new NullPointerException(); + }; + assertThrows(NullPointerException.class, executable); + } + } + """, + """ + import org.assertj.core.api.ThrowableAssert.ThrowingCallable; + + import static org.junit.jupiter.api.Assertions.assertThrows; + + public class SimpleExpectedExceptionTest { + public void throwsExceptionWithSpecificType() { + ThrowingCallable executable = () -> { + throw new NullPointerException(); + }; + assertThatExceptionOfType(NullPointerException.class).isThrownBy(executable); + } + } + """ + ) + ); + } }