Simplify suppressed exception assertions

See https://github.com/assertj/assertj/issues/3858
This commit is contained in:
Sam Brannen
2025-06-16 16:32:24 +02:00
parent 764d35c072
commit cc7dc47c4c

View File

@@ -21,7 +21,9 @@ import java.io.IOException;
import java.time.Duration; import java.time.Duration;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import org.assertj.core.api.ThrowingConsumer;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
@@ -32,7 +34,6 @@ import org.springframework.util.backoff.FixedBackOff;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.InstanceOfAssertFactories.array;
import static org.junit.jupiter.params.provider.Arguments.argumentSet; import static org.junit.jupiter.params.provider.Arguments.argumentSet;
/** /**
@@ -176,11 +177,10 @@ class RetryTemplateTests {
.isThrownBy(() -> retryTemplate.execute(retryable)) .isThrownBy(() -> retryTemplate.execute(retryable))
.withMessage("Retry policy for operation 'test' exhausted; aborting execution") .withMessage("Retry policy for operation 'test' exhausted; aborting execution")
.withCauseExactlyInstanceOf(IllegalStateException.class) .withCauseExactlyInstanceOf(IllegalStateException.class)
.extracting(Throwable::getSuppressed, array(Throwable[].class)) .satisfies(hasSuppressedExceptionsSatisfyingExactly(
.satisfiesExactly(
suppressed1 -> assertThat(suppressed1).isExactlyInstanceOf(IOException.class), suppressed1 -> assertThat(suppressed1).isExactlyInstanceOf(IOException.class),
suppressed2 -> assertThat(suppressed2).isExactlyInstanceOf(FileNotFoundException.class) suppressed2 -> assertThat(suppressed2).isExactlyInstanceOf(FileNotFoundException.class)
); ));
// 3 = 1 initial invocation + 2 retry attempts // 3 = 1 initial invocation + 2 retry attempts
assertThat(invocationCount).hasValue(3); assertThat(invocationCount).hasValue(3);
} }
@@ -228,16 +228,22 @@ class RetryTemplateTests {
.isThrownBy(() -> retryTemplate.execute(retryable)) .isThrownBy(() -> retryTemplate.execute(retryable))
.withMessage("Retry policy for operation 'test' exhausted; aborting execution") .withMessage("Retry policy for operation 'test' exhausted; aborting execution")
.withCauseExactlyInstanceOf(CustomFileNotFoundException.class) .withCauseExactlyInstanceOf(CustomFileNotFoundException.class)
.extracting(Throwable::getSuppressed, array(Throwable[].class)) .satisfies(hasSuppressedExceptionsSatisfyingExactly(
.satisfiesExactly(
suppressed1 -> assertThat(suppressed1).isExactlyInstanceOf(IOException.class), suppressed1 -> assertThat(suppressed1).isExactlyInstanceOf(IOException.class),
suppressed2 -> assertThat(suppressed2).isExactlyInstanceOf(IOException.class) suppressed2 -> assertThat(suppressed2).isExactlyInstanceOf(IOException.class)
); ));
// 3 = 1 initial invocation + 2 retry attempts // 3 = 1 initial invocation + 2 retry attempts
assertThat(invocationCount).hasValue(3); assertThat(invocationCount).hasValue(3);
} }
@SafeVarargs
private static final Consumer<Throwable> hasSuppressedExceptionsSatisfyingExactly(
ThrowingConsumer<? super Throwable>... requirements) {
return throwable -> assertThat(throwable.getSuppressed()).satisfiesExactly(requirements);
}
@SuppressWarnings("serial") @SuppressWarnings("serial")
private static class CustomFileNotFoundException extends FileNotFoundException { private static class CustomFileNotFoundException extends FileNotFoundException {
} }