Skip to content

Commit

Permalink
Add Throwable Stacktrace to ShouldHaveCauseInstance
Browse files Browse the repository at this point in the history
Fix #3392

(cherry picked from commit acd282b)
  • Loading branch information
shaikhu authored and joel-costigliola committed Apr 27, 2024
1 parent 44a5689 commit 9206a11
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 33 deletions.
Expand Up @@ -33,7 +33,7 @@ public class ShouldHaveCauseInstance extends BasicErrorMessageFactory {
public static ErrorMessageFactory shouldHaveCauseInstance(Throwable actual,
Class<? extends Throwable> expectedCauseType) {
return actual.getCause() == null
? new ShouldHaveCauseInstance(expectedCauseType)
? new ShouldHaveCauseInstance(expectedCauseType, actual)
: new ShouldHaveCauseInstance(actual, expectedCauseType);
}

Expand All @@ -47,10 +47,10 @@ private ShouldHaveCauseInstance(Throwable actual, Class<? extends Throwable> exp
expectedCauseType, actual.getCause().getClass());
}

private ShouldHaveCauseInstance(Class<? extends Throwable> expectedCauseType) {
private ShouldHaveCauseInstance(Class<? extends Throwable> 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);
}
}
Expand Up @@ -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
Expand All @@ -50,6 +51,5 @@ void should_create_error_message_for_wrong_cause() {
"Throwable that failed the check:%n" +
"%n" +
"%s", getStackTrace(actual)));

}
}
Expand Up @@ -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;

Expand All @@ -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<? extends Throwable> 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<NullPointerException> 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<NullPointerException> 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));
}
}

0 comments on commit 9206a11

Please sign in to comment.