Skip to content

Commit

Permalink
Fix assertj#1610: replace catchThrowable + isInstanceOf(AssertionErro…
Browse files Browse the repository at this point in the history
…r.class) by expectAssertionError
  • Loading branch information
cgrabmann authored and joel-costigliola committed Sep 22, 2019
1 parent b9074a6 commit 572b0ba
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.catchThrowable;
import static org.assertj.core.api.Assertions.catchThrowableOfType;
import static org.assertj.core.util.AssertionsUtil.assertThatAssertionErrorIsThrownBy;
import static org.assertj.core.util.AssertionsUtil.expectAssertionError;
import static org.assertj.core.util.Throwables.getStackTrace;

Expand All @@ -42,12 +43,12 @@ public void should_build_ThrowableAssert_with_throwable_thrown() {

@Test
public void should_be_able_to_pass_a_description_to_assertThatThrownBy() {
Throwable assertionError = catchThrowable(() -> {
// make assertThatThrownBy fail to verify the description afterwards
assertThatThrownBy(raisingException("boom"), "Test %s", "code").hasMessage("bam");
});
assertThat(assertionError).isInstanceOf(AssertionError.class)
.hasMessageContaining("[Test code]");
// GIVEN a failing assertion with a description
ThrowingCallable code = () -> assertThatThrownBy(raisingException("boom"), "Test %s", "code").hasMessage("bam");
// WHEN
AssertionError assertionError = expectAssertionError(code);
// THEN
assertThat(assertionError).hasMessageContaining("[Test code]");
}

@Test
Expand All @@ -61,7 +62,6 @@ public void can_capture_exception_and_then_assert_following_AAA_or_BDD_style() {
// when
Exception exception = new Exception("boom!!");
Throwable boom = catchThrowable(codeThrowing(exception));

// then
assertThat(boom).isSameAs(exception);
}
Expand All @@ -70,7 +70,6 @@ public void can_capture_exception_and_then_assert_following_AAA_or_BDD_style() {
public void catchThrowable_returns_null_when_no_exception_thrown() {
// when
Throwable boom = catchThrowable(() -> {});

// then
assertThat(boom).isNull();
}
Expand All @@ -84,8 +83,8 @@ public void catchThrowableOfType_should_fail_with_a_message_containing_the_origi
AssertionError assertionError = expectAssertionError(() -> catchThrowableOfType(codeThrowingException, IOException.class));
// THEN
assertThat(assertionError).hasMessageContainingAll(IOException.class.getName(),
Exception.class.getName(),
getStackTrace(exception));
Exception.class.getName(),
getStackTrace(exception));
}

@Test
Expand All @@ -106,25 +105,27 @@ public void catchThrowableOfType_should_succeed_and_return_null_if_no_exception_

@Test
public void should_fail_with_good_message_when_assertion_is_failing() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertThatThrownBy(raisingException("boom")).hasMessage("bam"))
.withMessageContainingAll("Expecting message to be:",
"<\"bam\">",
"but was:",
"<\"boom\">");
// GIVEN
ThrowingCallable code = () -> assertThatThrownBy(raisingException("boom")).hasMessage("bam");
// THEN
assertThatAssertionErrorIsThrownBy(code).withMessageContainingAll("Expecting message to be:",
"<\"bam\">",
"but was:",
"<\"boom\">");
}

@Test
public void should_fail_with_good_message_when_vararg_has_message_containing_assertion_is_failing() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertThatThrownBy(raisingException("boom")).hasMessageContaining("bam")
.hasMessageContainingAll("boom",
"Expecting:",
"<\"boom\">",
"<[\"bam\", \"boom\"]>",
"but could not find:",
"<[\"bam\"]>"));
// GIVEN
ThrowingCallable code = () -> assertThatThrownBy(raisingException("boom")).hasMessageContaining("%s", "bam");
// THEN
assertThatAssertionErrorIsThrownBy(code).withMessageContainingAll("Expecting:",
"<\"boom\">",
"to contain",
"<\"bam\">");
}

private ThrowingCallable raisingException(final String reason) {
private static ThrowingCallable raisingException(final String reason) {
return codeThrowing(new Exception(reason));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.catchThrowable;
import static org.assertj.core.data.TolkienCharacter.Race.DWARF;
import static org.assertj.core.data.TolkienCharacter.Race.ELF;
import static org.assertj.core.data.TolkienCharacter.Race.HOBBIT;
Expand All @@ -28,6 +27,7 @@

import org.assertj.core.api.AbstractAssertBaseTest;
import org.assertj.core.api.ConcreteAssert;
import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
import org.assertj.core.data.TolkienCharacter;
import org.assertj.core.description.TextDescription;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -102,23 +102,23 @@ public void should_honor_description() {
// GIVEN
Consumer<String> isEmpty = string -> assertThat(string).isEmpty();
Consumer<String> endsWithZ = string -> assertThat(string).endsWith("Z");
ThrowingCallable failingAssertionCode = () -> assertThat("abc").as("String checks").satisfiesAnyOf(isEmpty, endsWithZ);
// THEN
Throwable thrown = catchThrowable(() -> assertThat("abc").as("String checks").satisfiesAnyOf(isEmpty, endsWithZ));
AssertionError assertionError = expectAssertionError(failingAssertionCode);
// THEN
assertThat(thrown).isInstanceOf(AssertionError.class)
.hasMessageContaining("String checks");
assertThat(assertionError).hasMessageContaining("String checks");
}

@Test
public void should_not_honor_overriding_error_message() {
// GIVEN
Consumer<String> isEmpty = string -> assertThat(string).isEmpty();
Consumer<String> isEmpty = string -> assertThat(string).overridingErrorMessage("fail empty").isEmpty();
Consumer<String> endsWithZ = string -> assertThat(string).endsWith("Z");
ThrowingCallable failingAssertionCode = () -> assertThat("abc").satisfiesAnyOf(isEmpty, endsWithZ);
// THEN
Throwable thrown = catchThrowable(() -> assertThat("abc").as("String checks").satisfiesAnyOf(isEmpty, endsWithZ));
AssertionError assertionError = expectAssertionError(failingAssertionCode);
// THEN
assertThat(thrown).isInstanceOf(AssertionError.class)
.hasMessageContaining("String checks");
assertThat(assertionError).hasMessageContaining("fail empty");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
import static java.lang.String.format;
import static java.util.concurrent.CompletableFuture.completedFuture;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.catchThrowable;
import static org.assertj.core.error.future.ShouldBeCompleted.shouldBeCompleted;
import static org.assertj.core.error.future.Warning.WARNING;
import static org.assertj.core.util.AssertionsUtil.expectAssertionError;
import static org.assertj.core.util.FailureMessages.actualIsNull;

import java.util.concurrent.CompletableFuture;
Expand All @@ -40,47 +40,43 @@ public void should_fail_when_completable_future_is_null() {
// GIVEN
CompletableFuture<String> future = null;
// WHEN
Throwable throwable = catchThrowable(() -> assertThat(future).isCompletedWithValueMatching(result -> result.equals("done")));
AssertionError assertionError = expectAssertionError(() -> assertThat(future).isCompletedWithValueMatching(result -> result.equals("done")));
// THEN
assertThat(throwable).isInstanceOf(AssertionError.class)
.hasMessage(format(actualIsNull()));
assertThat(assertionError).hasMessage(format(actualIsNull()));
}

@Test
public void should_fail_if_result_does_not_match() {
// GIVEN
CompletableFuture<String> future = CompletableFuture.completedFuture("done");
// WHEN
Throwable throwable = catchThrowable(() -> assertThat(future).isCompletedWithValueMatching(result -> result.equals("foo"),
"is foo"));
AssertionError assertionError = expectAssertionError(() -> assertThat(future).isCompletedWithValueMatching(result -> result.equals("foo"),
"is foo"));
// THEN
assertThat(throwable).isInstanceOf(AssertionError.class)
.hasMessageContaining("<\"done\">")
.hasMessageContaining("to match 'is foo' predicate");
assertThat(assertionError).hasMessageContaining("<\"done\">")
.hasMessageContaining("to match 'is foo' predicate");
}

@Test
public void should_print_advice_without_description() {
// GIVEN
CompletableFuture<String> future = CompletableFuture.completedFuture("done");
// WHEN
Throwable throwable = catchThrowable(() -> assertThat(future).isCompletedWithValueMatching(result -> result.equals("foo")));
AssertionError assertionError = expectAssertionError(() -> assertThat(future).isCompletedWithValueMatching(result -> result.equals("foo")));
// THEN
assertThat(throwable).isInstanceOf(AssertionError.class)
.hasMessageContaining("<\"done\">")
.hasMessageContaining("to match given predicate")
.hasMessageContaining("a better error message");
assertThat(assertionError).hasMessageContaining("<\"done\">")
.hasMessageContaining("to match given predicate")
.hasMessageContaining("a better error message");
}

@Test
public void should_fail_if_completable_future_is_incomplete() {
// GIVEN
CompletableFuture<String> future = new CompletableFuture<>();
// WHEN
Throwable throwable = catchThrowable(() -> assertThat(future).isCompletedWithValueMatching(result -> result.equals("done")));
AssertionError assertionError = expectAssertionError(() -> assertThat(future).isCompletedWithValueMatching(result -> result.equals("done")));
// THEN
assertThat(throwable).isInstanceOf(AssertionError.class)
.hasMessage(shouldBeCompleted(future).create());
assertThat(assertionError).hasMessage(shouldBeCompleted(future).create());
}

@Test
Expand All @@ -89,12 +85,11 @@ public void should_fail_if_completable_future_has_failed() {
CompletableFuture<String> future = new CompletableFuture<>();
future.completeExceptionally(new RuntimeException());
// WHEN
Throwable throwable = catchThrowable(() -> assertThat(future).isCompletedWithValueMatching(result -> result.equals("done")));
AssertionError assertionError = expectAssertionError(() -> assertThat(future).isCompletedWithValueMatching(result -> result.equals("done")));
// THEN
assertThat(throwable).isInstanceOf(AssertionError.class)
.hasMessageStartingWith(format("%nExpecting%n <CompletableFuture[Failed: java.lang.RuntimeException]%n"))
.hasMessageContaining("Caused by: java.lang.RuntimeException")
.hasMessageEndingWith("to be completed.%n%s", WARNING);
assertThat(assertionError).hasMessageStartingWith(format("%nExpecting%n <CompletableFuture[Failed: java.lang.RuntimeException]%n"))
.hasMessageContaining("Caused by: java.lang.RuntimeException")
.hasMessageEndingWith("to be completed.%n%s", WARNING);

}

Expand All @@ -104,9 +99,8 @@ public void should_fail_if_completable_future_was_cancelled() {
CompletableFuture<String> future = new CompletableFuture<>();
future.cancel(true);
// WHEN
Throwable throwable = catchThrowable(() -> assertThat(future).isCompletedWithValueMatching(result -> result.equals("done")));
AssertionError assertionError = expectAssertionError(() -> assertThat(future).isCompletedWithValueMatching(result -> result.equals("done")));
// THEN
assertThat(throwable).isInstanceOf(AssertionError.class)
.hasMessage(shouldBeCompleted(future).create());
assertThat(assertionError).hasMessage(shouldBeCompleted(future).create());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.catchThrowable;
import static org.assertj.core.error.future.ShouldBeCompleted.shouldBeCompleted;
import static org.assertj.core.error.future.Warning.WARNING;
import static org.assertj.core.util.AssertionsUtil.expectAssertionError;
import static org.assertj.core.util.FailureMessages.actualIsNull;

import java.util.concurrent.CompletableFuture;
Expand All @@ -39,33 +39,30 @@ public void should_fail_when_completable_future_is_null() {
// GIVEN
CompletableFuture<String> future = null;
// WHEN
Throwable throwable = catchThrowable(() -> assertThat(future).isCompletedWithValue("foo"));
AssertionError assertionError = expectAssertionError(() -> assertThat(future).isCompletedWithValue("foo"));
// THEN
assertThat(throwable).isInstanceOf(AssertionError.class)
.hasMessage(format(actualIsNull()));
assertThat(assertionError).hasMessage(format(actualIsNull()));
}

@Test
public void should_fail_if_result_does_not_match() {
// GIVEN
CompletableFuture<String> future = CompletableFuture.completedFuture("done");
// WHEN
Throwable throwable = catchThrowable(() -> assertThat(future).isCompletedWithValue("foo"));
AssertionError assertionError = expectAssertionError(() -> assertThat(future).isCompletedWithValue("foo"));
// THEN
assertThat(throwable).isInstanceOf(AssertionError.class)
.hasMessageContaining("foo")
.hasMessageContaining("done");
assertThat(assertionError).hasMessageContaining("foo")
.hasMessageContaining("done");
}

@Test
public void should_fail_if_completable_future_is_incomplete() {
// GIVEN
CompletableFuture<String> future = new CompletableFuture<>();
// WHEN
Throwable throwable = catchThrowable(() -> assertThat(future).isCompletedWithValue("done"));
AssertionError assertionError = expectAssertionError(() -> assertThat(future).isCompletedWithValue("done"));
// THEN
assertThat(throwable).isInstanceOf(AssertionError.class)
.hasMessage(shouldBeCompleted(future).create());
assertThat(assertionError).hasMessage(shouldBeCompleted(future).create());
}

@Test
Expand All @@ -74,12 +71,11 @@ public void should_fail_if_completable_future_has_failed() {
CompletableFuture<String> future = new CompletableFuture<>();
future.completeExceptionally(new RuntimeException());
// WHEN
Throwable throwable = catchThrowable(() -> assertThat(future).isCompletedWithValue("done"));
AssertionError assertionError = expectAssertionError(() -> assertThat(future).isCompletedWithValue("done"));
// THEN
assertThat(throwable).isInstanceOf(AssertionError.class)
.hasMessageStartingWith(format("%nExpecting%n <CompletableFuture[Failed: java.lang.RuntimeException]%n"))
.hasMessageContaining("Caused by: java.lang.RuntimeException")
.hasMessageEndingWith("to be completed.%n%s", WARNING);
assertThat(assertionError).hasMessageStartingWith(format("%nExpecting%n <CompletableFuture[Failed: java.lang.RuntimeException]%n"))
.hasMessageContaining("Caused by: java.lang.RuntimeException")
.hasMessageEndingWith("to be completed.%n%s", WARNING);

}

Expand All @@ -89,9 +85,8 @@ public void should_fail_if_completable_future_was_cancelled() {
CompletableFuture<String> future = new CompletableFuture<>();
future.cancel(true);
// WHEN
Throwable throwable = catchThrowable(() -> assertThat(future).isCompletedWithValue("done"));
AssertionError assertionError = expectAssertionError(() -> assertThat(future).isCompletedWithValue("done"));
// THEN
assertThat(throwable).isInstanceOf(AssertionError.class)
.hasMessage(shouldBeCompleted(future).create());
assertThat(assertionError).hasMessage(shouldBeCompleted(future).create());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
package org.assertj.core.api.optional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;
import static org.assertj.core.error.OptionalShouldBePresent.shouldBePresent;
import static org.assertj.core.error.OptionalShouldContainInstanceOf.shouldContainInstanceOf;
import static org.assertj.core.util.AssertionsUtil.expectAssertionError;

import java.util.Optional;

Expand All @@ -26,14 +26,12 @@ public class OptionalAssert_containsInstanceOf_Test extends BaseTest {

@Test
public void should_fail_if_optional_is_empty() {
// GIVEN
Optional<Object> actual = Optional.empty();

Throwable thrown = catchThrowable(() -> {
assertThat(actual).containsInstanceOf(Object.class);
});

assertThat(thrown).isInstanceOf(AssertionError.class)
.hasMessage(shouldBePresent(actual).create());
// WHEN
AssertionError assertionError = expectAssertionError(() -> assertThat(actual).containsInstanceOf(Object.class));
// THEN
assertThat(assertionError).hasMessage(shouldBePresent(actual).create());
}

@Test
Expand All @@ -49,14 +47,12 @@ public void should_pass_if_optional_contains_required_type_subclass() {

@Test
public void should_fail_if_optional_contains_other_type_than_required() {
// GIVEN
Optional<ParentClass> actual = Optional.of(new ParentClass());

Throwable thrown = catchThrowable(() -> {
assertThat(actual).containsInstanceOf(OtherClass.class);
});

assertThat(thrown).isInstanceOf(AssertionError.class)
.hasMessage(shouldContainInstanceOf(actual, OtherClass.class).create());
// WHEN
AssertionError assertionError = expectAssertionError(() -> assertThat(actual).containsInstanceOf(OtherClass.class));
// THEN
assertThat(assertionError).hasMessage(shouldContainInstanceOf(actual, OtherClass.class).create());
}

private static class ParentClass {
Expand All @@ -65,4 +61,4 @@ private static class SubClass extends ParentClass {
}
private static class OtherClass {
}
}
}
Loading

0 comments on commit 572b0ba

Please sign in to comment.