Skip to content

Commit cc7dc47

Browse files
committed
Simplify suppressed exception assertions
See assertj/assertj#3858
1 parent 764d35c commit cc7dc47

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

spring-core/src/test/java/org/springframework/core/retry/RetryTemplateTests.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
import java.time.Duration;
2222
import java.util.List;
2323
import java.util.concurrent.atomic.AtomicInteger;
24+
import java.util.function.Consumer;
2425

26+
import org.assertj.core.api.ThrowingConsumer;
2527
import org.junit.jupiter.api.BeforeEach;
2628
import org.junit.jupiter.api.Test;
2729
import org.junit.jupiter.params.ParameterizedTest;
@@ -32,7 +34,6 @@
3234

3335
import static org.assertj.core.api.Assertions.assertThat;
3436
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
35-
import static org.assertj.core.api.InstanceOfAssertFactories.array;
3637
import static org.junit.jupiter.params.provider.Arguments.argumentSet;
3738

3839
/**
@@ -176,11 +177,10 @@ public String getName() {
176177
.isThrownBy(() -> retryTemplate.execute(retryable))
177178
.withMessage("Retry policy for operation 'test' exhausted; aborting execution")
178179
.withCauseExactlyInstanceOf(IllegalStateException.class)
179-
.extracting(Throwable::getSuppressed, array(Throwable[].class))
180-
.satisfiesExactly(
180+
.satisfies(hasSuppressedExceptionsSatisfyingExactly(
181181
suppressed1 -> assertThat(suppressed1).isExactlyInstanceOf(IOException.class),
182182
suppressed2 -> assertThat(suppressed2).isExactlyInstanceOf(FileNotFoundException.class)
183-
);
183+
));
184184
// 3 = 1 initial invocation + 2 retry attempts
185185
assertThat(invocationCount).hasValue(3);
186186
}
@@ -228,16 +228,22 @@ public String getName() {
228228
.isThrownBy(() -> retryTemplate.execute(retryable))
229229
.withMessage("Retry policy for operation 'test' exhausted; aborting execution")
230230
.withCauseExactlyInstanceOf(CustomFileNotFoundException.class)
231-
.extracting(Throwable::getSuppressed, array(Throwable[].class))
232-
.satisfiesExactly(
231+
.satisfies(hasSuppressedExceptionsSatisfyingExactly(
233232
suppressed1 -> assertThat(suppressed1).isExactlyInstanceOf(IOException.class),
234233
suppressed2 -> assertThat(suppressed2).isExactlyInstanceOf(IOException.class)
235-
);
234+
));
236235
// 3 = 1 initial invocation + 2 retry attempts
237236
assertThat(invocationCount).hasValue(3);
238237
}
239238

240239

240+
@SafeVarargs
241+
private static final Consumer<Throwable> hasSuppressedExceptionsSatisfyingExactly(
242+
ThrowingConsumer<? super Throwable>... requirements) {
243+
return throwable -> assertThat(throwable.getSuppressed()).satisfiesExactly(requirements);
244+
}
245+
246+
241247
@SuppressWarnings("serial")
242248
private static class CustomFileNotFoundException extends FileNotFoundException {
243249
}

0 commit comments

Comments
 (0)