@@ -139,7 +139,7 @@ public String getName() {
139
139
}
140
140
141
141
@ Test
142
- void retryWithExceptionIncludes () throws Exception {
142
+ void retryWithExceptionIncludes () {
143
143
var invocationCount = new AtomicInteger ();
144
144
145
145
var retryable = new Retryable <>() {
@@ -181,7 +181,49 @@ public String getName() {
181
181
}
182
182
183
183
@ 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 () {
185
227
var invocationCount = new AtomicInteger ();
186
228
187
229
var retryable = new Retryable <>() {
@@ -223,6 +265,7 @@ public String getName() {
223
265
assertThat (invocationCount ).hasValue (3 );
224
266
}
225
267
268
+
226
269
@ SuppressWarnings ("serial" )
227
270
private static class CustomFileNotFoundException extends FileNotFoundException {
228
271
}
0 commit comments