From acd282b72ccf4fe7cce17da565df8f8a4f67723b Mon Sep 17 00:00:00 2001 From: Usman Shaikh Date: Sat, 9 Mar 2024 15:12:27 +0000 Subject: [PATCH] Add Throwable Stacktrace to ShouldHaveCauseInstance Fix #3392 --- .../core/error/ShouldHaveCauseInstance.java | 8 +-- .../ShouldHaveCauseInstance_create_Test.java | 6 +- ...owables_assertHasCauseInstanceOf_Test.java | 55 ++++++++++--------- 3 files changed, 36 insertions(+), 33 deletions(-) diff --git a/assertj-core/src/main/java/org/assertj/core/error/ShouldHaveCauseInstance.java b/assertj-core/src/main/java/org/assertj/core/error/ShouldHaveCauseInstance.java index 5707ca0e94..ebed096c76 100644 --- a/assertj-core/src/main/java/org/assertj/core/error/ShouldHaveCauseInstance.java +++ b/assertj-core/src/main/java/org/assertj/core/error/ShouldHaveCauseInstance.java @@ -33,7 +33,7 @@ public class ShouldHaveCauseInstance extends BasicErrorMessageFactory { public static ErrorMessageFactory shouldHaveCauseInstance(Throwable actual, Class expectedCauseType) { return actual.getCause() == null - ? new ShouldHaveCauseInstance(expectedCauseType) + ? new ShouldHaveCauseInstance(expectedCauseType, actual) : new ShouldHaveCauseInstance(actual, expectedCauseType); } @@ -47,10 +47,10 @@ private ShouldHaveCauseInstance(Throwable actual, Class exp expectedCauseType, actual.getCause().getClass()); } - private ShouldHaveCauseInstance(Class expectedCauseType) { + private ShouldHaveCauseInstance(Class expectedCauseType, Throwable actual) { super("%nExpecting a throwable with cause being an instance of:%n" + " %s%n" + - "but current throwable has no cause.", - expectedCauseType); + "but current throwable has no cause.%n" + + "Throwable that failed the check:%n" + escapePercent(getStackTrace(actual)), expectedCauseType); } } diff --git a/assertj-core/src/test/java/org/assertj/core/error/ShouldHaveCauseInstance_create_Test.java b/assertj-core/src/test/java/org/assertj/core/error/ShouldHaveCauseInstance_create_Test.java index e3bf679af0..ae4281b4ab 100644 --- a/assertj-core/src/test/java/org/assertj/core/error/ShouldHaveCauseInstance_create_Test.java +++ b/assertj-core/src/test/java/org/assertj/core/error/ShouldHaveCauseInstance_create_Test.java @@ -24,14 +24,15 @@ class ShouldHaveCauseInstance_create_Test { @Test void should_create_error_message_for_no_cause() { // GIVEN - Throwable actual = new RuntimeException(); + Throwable actual = new RuntimeException("boom %s"); Throwable expected = new IllegalStateException(); // WHEN String message = shouldHaveCauseInstance(actual, expected.getClass()).create(); // THEN then(message).isEqualTo("%nExpecting a throwable with cause being an instance of:%n" + " %s%n" + - "but current throwable has no cause.", expected); + "but current throwable has no cause." + + "%nThrowable that failed the check:%n%s", expected, getStackTrace(actual)); } @Test @@ -50,6 +51,5 @@ void should_create_error_message_for_wrong_cause() { "Throwable that failed the check:%n" + "%n" + "%s", getStackTrace(actual))); - } } diff --git a/assertj-core/src/test/java/org/assertj/core/internal/throwables/Throwables_assertHasCauseInstanceOf_Test.java b/assertj-core/src/test/java/org/assertj/core/internal/throwables/Throwables_assertHasCauseInstanceOf_Test.java index 74dd404a32..d681151e04 100644 --- a/assertj-core/src/test/java/org/assertj/core/internal/throwables/Throwables_assertHasCauseInstanceOf_Test.java +++ b/assertj-core/src/test/java/org/assertj/core/internal/throwables/Throwables_assertHasCauseInstanceOf_Test.java @@ -12,16 +12,13 @@ */ package org.assertj.core.internal.throwables; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import static org.assertj.core.api.Assertions.assertThatNullPointerException; import static org.assertj.core.api.Assertions.catchThrowable; +import static org.assertj.core.api.BDDAssertions.then; import static org.assertj.core.error.ShouldHaveCauseInstance.shouldHaveCauseInstance; -import static org.assertj.core.test.TestData.someInfo; +import static org.assertj.core.util.AssertionsUtil.expectAssertionError; import static org.assertj.core.util.FailureMessages.actualIsNull; import static org.mockito.Mockito.verify; -import org.assertj.core.api.AssertionInfo; import org.assertj.core.internal.ThrowablesBaseTest; import org.junit.jupiter.api.Test; @@ -34,51 +31,57 @@ */ class Throwables_assertHasCauseInstanceOf_Test extends ThrowablesBaseTest { - private Throwable throwableWithCause = new Throwable(new IllegalArgumentException()); + private final Throwable throwableWithCause = new Throwable(new IllegalArgumentException()); @Test void should_pass_if_cause_is_exactly_instance_of_expected_type() { - throwables.assertHasCauseInstanceOf(someInfo(), throwableWithCause, IllegalArgumentException.class); + throwables.assertHasCauseInstanceOf(INFO, throwableWithCause, IllegalArgumentException.class); } @Test void should_pass_if_cause_is_instance_of_expected_type() { - throwables.assertHasCauseInstanceOf(someInfo(), throwableWithCause, RuntimeException.class); + throwables.assertHasCauseInstanceOf(INFO, throwableWithCause, RuntimeException.class); } @Test void should_fail_if_actual_is_null() { - assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> throwables.assertHasCauseInstanceOf(someInfo(), null, - IllegalArgumentException.class)) - .withMessage(actualIsNull()); + // GIVEN + Throwable actual = null; + // WHEN + AssertionError error = expectAssertionError(() -> throwables.assertHasCauseInstanceOf(INFO, actual, + IllegalArgumentException.class)); + // THEN + then(error).hasMessage(actualIsNull()); } @Test void should_throw_NullPointerException_if_given_type_is_null() { - assertThatNullPointerException().isThrownBy(() -> throwables.assertHasCauseInstanceOf(someInfo(), - throwableWithCause, null)) - .withMessage("The given type should not be null"); + // GIVEN + Class type = null; + // WHEN + Throwable throwable = catchThrowable(() -> throwables.assertHasCauseInstanceOf(INFO, throwableWithCause, type)); + // THEN + then(throwable).isInstanceOf(NullPointerException.class) + .hasMessage("The given type should not be null"); } @Test void should_fail_if_actual_has_no_cause() { - AssertionInfo info = someInfo(); + // GIVEN Class expectedCauseType = NullPointerException.class; - - Throwable error = catchThrowable(() -> throwables.assertHasCauseInstanceOf(info, actual, expectedCauseType)); - - assertThat(error).isInstanceOf(AssertionError.class); - verify(failures).failure(info, shouldHaveCauseInstance(actual, expectedCauseType)); + // WHEN + expectAssertionError(() -> throwables.assertHasCauseInstanceOf(INFO, actual, expectedCauseType)); + // THEN + verify(failures).failure(INFO, shouldHaveCauseInstance(actual, expectedCauseType)); } @Test void should_fail_if_cause_is_not_instance_of_expected_type() { - AssertionInfo info = someInfo(); + // GIVEN Class expectedCauseType = NullPointerException.class; - - Throwable error = catchThrowable(() -> throwables.assertHasCauseInstanceOf(info, throwableWithCause, expectedCauseType)); - - assertThat(error).isInstanceOf(AssertionError.class); - verify(failures).failure(info, shouldHaveCauseInstance(throwableWithCause, expectedCauseType)); + // WHEN + expectAssertionError(() -> throwables.assertHasCauseInstanceOf(INFO, throwableWithCause, expectedCauseType)); + // THEN + verify(failures).failure(INFO, shouldHaveCauseInstance(throwableWithCause, expectedCauseType)); } }