From cc7dc47c4c2ddee2aa40b13db1c2f74be0585b5e Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Mon, 16 Jun 2025 16:32:24 +0200 Subject: [PATCH] Simplify suppressed exception assertions See https://github.com/assertj/assertj/issues/3858 --- .../core/retry/RetryTemplateTests.java | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/spring-core/src/test/java/org/springframework/core/retry/RetryTemplateTests.java b/spring-core/src/test/java/org/springframework/core/retry/RetryTemplateTests.java index 8ac5ad919d9..6c1e88d92c3 100644 --- a/spring-core/src/test/java/org/springframework/core/retry/RetryTemplateTests.java +++ b/spring-core/src/test/java/org/springframework/core/retry/RetryTemplateTests.java @@ -21,7 +21,9 @@ import java.io.IOException; import java.time.Duration; import java.util.List; 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.Test; 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.assertThatExceptionOfType; -import static org.assertj.core.api.InstanceOfAssertFactories.array; import static org.junit.jupiter.params.provider.Arguments.argumentSet; /** @@ -176,11 +177,10 @@ class RetryTemplateTests { .isThrownBy(() -> retryTemplate.execute(retryable)) .withMessage("Retry policy for operation 'test' exhausted; aborting execution") .withCauseExactlyInstanceOf(IllegalStateException.class) - .extracting(Throwable::getSuppressed, array(Throwable[].class)) - .satisfiesExactly( + .satisfies(hasSuppressedExceptionsSatisfyingExactly( suppressed1 -> assertThat(suppressed1).isExactlyInstanceOf(IOException.class), suppressed2 -> assertThat(suppressed2).isExactlyInstanceOf(FileNotFoundException.class) - ); + )); // 3 = 1 initial invocation + 2 retry attempts assertThat(invocationCount).hasValue(3); } @@ -228,16 +228,22 @@ class RetryTemplateTests { .isThrownBy(() -> retryTemplate.execute(retryable)) .withMessage("Retry policy for operation 'test' exhausted; aborting execution") .withCauseExactlyInstanceOf(CustomFileNotFoundException.class) - .extracting(Throwable::getSuppressed, array(Throwable[].class)) - .satisfiesExactly( + .satisfies(hasSuppressedExceptionsSatisfyingExactly( suppressed1 -> assertThat(suppressed1).isExactlyInstanceOf(IOException.class), suppressed2 -> assertThat(suppressed2).isExactlyInstanceOf(IOException.class) - ); + )); // 3 = 1 initial invocation + 2 retry attempts assertThat(invocationCount).hasValue(3); } + @SafeVarargs + private static final Consumer hasSuppressedExceptionsSatisfyingExactly( + ThrowingConsumer... requirements) { + return throwable -> assertThat(throwable.getSuppressed()).satisfiesExactly(requirements); + } + + @SuppressWarnings("serial") private static class CustomFileNotFoundException extends FileNotFoundException { }