Skip to content

Commit 09372b7

Browse files
committed
Add retryWithExceptionExcludes() test
1 parent 97522cf commit 09372b7

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

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

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public String getName() {
139139
}
140140

141141
@Test
142-
void retryWithExceptionIncludes() throws Exception {
142+
void retryWithExceptionIncludes() {
143143
var invocationCount = new AtomicInteger();
144144

145145
var retryable = new Retryable<>() {
@@ -181,7 +181,49 @@ public String getName() {
181181
}
182182

183183
@Test
184-
void retryWithExceptionExcludes() throws Exception {
184+
void retryWithExceptionExcludes() {
185+
var invocationCount = new AtomicInteger();
186+
187+
var retryable = new Retryable<>() {
188+
@Override
189+
public String execute() throws Exception {
190+
return switch (invocationCount.incrementAndGet()) {
191+
case 1 -> throw new IOException();
192+
case 2 -> throw new IOException();
193+
case 3 -> throw new CustomFileNotFoundException();
194+
default -> "success";
195+
};
196+
}
197+
198+
@Override
199+
public String getName() {
200+
return "test";
201+
}
202+
};
203+
204+
var retryPolicy = RetryPolicy.builder()
205+
.maxAttempts(Integer.MAX_VALUE)
206+
.excludes(FileNotFoundException.class)
207+
.build();
208+
209+
retryTemplate.setRetryPolicy(retryPolicy);
210+
211+
assertThat(invocationCount).hasValue(0);
212+
assertThatExceptionOfType(RetryException.class)
213+
.isThrownBy(() -> retryTemplate.execute(retryable))
214+
.withMessage("Retry policy for operation 'test' exhausted; aborting execution")
215+
.withCauseExactlyInstanceOf(CustomFileNotFoundException.class)
216+
.extracting(Throwable::getSuppressed, array(Throwable[].class))
217+
.satisfiesExactly(
218+
suppressed1 -> assertThat(suppressed1).isExactlyInstanceOf(IOException.class),
219+
suppressed2 -> assertThat(suppressed2).isExactlyInstanceOf(IOException.class)
220+
);
221+
// 3 = 1 initial invocation + 2 retry attempts
222+
assertThat(invocationCount).hasValue(3);
223+
}
224+
225+
@Test
226+
void retryWithExceptionIncludesAndExcludes() {
185227
var invocationCount = new AtomicInteger();
186228

187229
var retryable = new Retryable<>() {
@@ -223,6 +265,7 @@ public String getName() {
223265
assertThat(invocationCount).hasValue(3);
224266
}
225267

268+
226269
@SuppressWarnings("serial")
227270
private static class CustomFileNotFoundException extends FileNotFoundException {
228271
}

0 commit comments

Comments
 (0)