19
19
import java .io .FileNotFoundException ;
20
20
import java .io .IOException ;
21
21
import java .time .Duration ;
22
+ import java .util .List ;
22
23
import java .util .concurrent .atomic .AtomicInteger ;
23
24
24
25
import org .junit .jupiter .api .BeforeEach ;
25
26
import org .junit .jupiter .api .Test ;
27
+ import org .junit .jupiter .params .ParameterizedTest ;
28
+ import org .junit .jupiter .params .provider .Arguments .ArgumentSet ;
29
+ import org .junit .jupiter .params .provider .FieldSource ;
26
30
27
31
import org .springframework .util .backoff .FixedBackOff ;
28
32
29
33
import static org .assertj .core .api .Assertions .assertThat ;
30
34
import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
31
35
import static org .assertj .core .api .InstanceOfAssertFactories .array ;
36
+ import static org .junit .jupiter .params .provider .Arguments .argumentSet ;
32
37
33
38
/**
34
39
* Tests for {@link RetryTemplate}.
@@ -180,50 +185,25 @@ public String getName() {
180
185
assertThat (invocationCount ).hasValue (3 );
181
186
}
182
187
183
- @ Test
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
-
188
+ static final List <ArgumentSet > includesAndExcludesRetryPolicies = List .of (
189
+ argumentSet ("Excludes" ,
190
+ RetryPolicy .builder ()
191
+ .maxAttempts (Integer .MAX_VALUE )
192
+ .excludes (FileNotFoundException .class )
193
+ .build ()),
194
+ argumentSet ("Includes & Excludes" ,
195
+ RetryPolicy .builder ()
196
+ .maxAttempts (Integer .MAX_VALUE )
197
+ .includes (IOException .class )
198
+ .excludes (FileNotFoundException .class )
199
+ .build ())
200
+ );
201
+
202
+ @ ParameterizedTest
203
+ @ FieldSource ("includesAndExcludesRetryPolicies" )
204
+ void retryWithIncludesAndExcludesRetryPolicies (RetryPolicy retryPolicy ) {
209
205
retryTemplate .setRetryPolicy (retryPolicy );
210
206
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 () {
227
207
var invocationCount = new AtomicInteger ();
228
208
229
209
var retryable = new Retryable <>() {
@@ -243,14 +223,6 @@ public String getName() {
243
223
}
244
224
};
245
225
246
- var retryPolicy = RetryPolicy .builder ()
247
- .maxAttempts (Integer .MAX_VALUE )
248
- .includes (IOException .class )
249
- .excludes (FileNotFoundException .class )
250
- .build ();
251
-
252
- retryTemplate .setRetryPolicy (retryPolicy );
253
-
254
226
assertThat (invocationCount ).hasValue (0 );
255
227
assertThatExceptionOfType (RetryException .class )
256
228
.isThrownBy (() -> retryTemplate .execute (retryable ))
0 commit comments