Skip to content

Commit 6bbfd56

Browse files
committed
Remove duplication in RetryTemplateTests
1 parent f41a568 commit 6bbfd56

File tree

1 file changed

+22
-50
lines changed

1 file changed

+22
-50
lines changed

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

Lines changed: 22 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,21 @@
1919
import java.io.FileNotFoundException;
2020
import java.io.IOException;
2121
import java.time.Duration;
22+
import java.util.List;
2223
import java.util.concurrent.atomic.AtomicInteger;
2324

2425
import org.junit.jupiter.api.BeforeEach;
2526
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;
2630

2731
import org.springframework.util.backoff.FixedBackOff;
2832

2933
import static org.assertj.core.api.Assertions.assertThat;
3034
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
3135
import static org.assertj.core.api.InstanceOfAssertFactories.array;
36+
import static org.junit.jupiter.params.provider.Arguments.argumentSet;
3237

3338
/**
3439
* Tests for {@link RetryTemplate}.
@@ -180,50 +185,25 @@ public String getName() {
180185
assertThat(invocationCount).hasValue(3);
181186
}
182187

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) {
209205
retryTemplate.setRetryPolicy(retryPolicy);
210206

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() {
227207
var invocationCount = new AtomicInteger();
228208

229209
var retryable = new Retryable<>() {
@@ -243,14 +223,6 @@ public String getName() {
243223
}
244224
};
245225

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-
254226
assertThat(invocationCount).hasValue(0);
255227
assertThatExceptionOfType(RetryException.class)
256228
.isThrownBy(() -> retryTemplate.execute(retryable))

0 commit comments

Comments
 (0)