From 6bbfd56dced92aa8465668ed3a28e740978a042d Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Mon, 16 Jun 2025 14:32:33 +0200 Subject: [PATCH] Remove duplication in RetryTemplateTests --- .../core/retry/RetryTemplateTests.java | 76 ++++++------------- 1 file changed, 24 insertions(+), 52 deletions(-) diff --git a/spring-core/src/test/java/org/springframework/core/retry/RetryTemplateTests.java b/spring-core/src/test/java/org/springframework/core/retry/RetryTemplateTests.java index 96383d9b9d..8ac5ad919d 100644 --- a/spring-core/src/test/java/org/springframework/core/retry/RetryTemplateTests.java +++ b/spring-core/src/test/java/org/springframework/core/retry/RetryTemplateTests.java @@ -19,16 +19,21 @@ package org.springframework.core.retry; import java.io.FileNotFoundException; import java.io.IOException; import java.time.Duration; +import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments.ArgumentSet; +import org.junit.jupiter.params.provider.FieldSource; import org.springframework.util.backoff.FixedBackOff; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.assertj.core.api.InstanceOfAssertFactories.array; +import static org.junit.jupiter.params.provider.Arguments.argumentSet; /** * Tests for {@link RetryTemplate}. @@ -180,8 +185,25 @@ class RetryTemplateTests { assertThat(invocationCount).hasValue(3); } - @Test - void retryWithExceptionExcludes() { + static final List includesAndExcludesRetryPolicies = List.of( + argumentSet("Excludes", + RetryPolicy.builder() + .maxAttempts(Integer.MAX_VALUE) + .excludes(FileNotFoundException.class) + .build()), + argumentSet("Includes & Excludes", + RetryPolicy.builder() + .maxAttempts(Integer.MAX_VALUE) + .includes(IOException.class) + .excludes(FileNotFoundException.class) + .build()) + ); + + @ParameterizedTest + @FieldSource("includesAndExcludesRetryPolicies") + void retryWithIncludesAndExcludesRetryPolicies(RetryPolicy retryPolicy) { + retryTemplate.setRetryPolicy(retryPolicy); + var invocationCount = new AtomicInteger(); var retryable = new Retryable<>() { @@ -201,56 +223,6 @@ class RetryTemplateTests { } }; - var retryPolicy = RetryPolicy.builder() - .maxAttempts(Integer.MAX_VALUE) - .excludes(FileNotFoundException.class) - .build(); - - retryTemplate.setRetryPolicy(retryPolicy); - - assertThat(invocationCount).hasValue(0); - assertThatExceptionOfType(RetryException.class) - .isThrownBy(() -> retryTemplate.execute(retryable)) - .withMessage("Retry policy for operation 'test' exhausted; aborting execution") - .withCauseExactlyInstanceOf(CustomFileNotFoundException.class) - .extracting(Throwable::getSuppressed, array(Throwable[].class)) - .satisfiesExactly( - suppressed1 -> assertThat(suppressed1).isExactlyInstanceOf(IOException.class), - suppressed2 -> assertThat(suppressed2).isExactlyInstanceOf(IOException.class) - ); - // 3 = 1 initial invocation + 2 retry attempts - assertThat(invocationCount).hasValue(3); - } - - @Test - void retryWithExceptionIncludesAndExcludes() { - var invocationCount = new AtomicInteger(); - - var retryable = new Retryable<>() { - @Override - public String execute() throws Exception { - return switch (invocationCount.incrementAndGet()) { - case 1 -> throw new IOException(); - case 2 -> throw new IOException(); - case 3 -> throw new CustomFileNotFoundException(); - default -> "success"; - }; - } - - @Override - public String getName() { - return "test"; - } - }; - - var retryPolicy = RetryPolicy.builder() - .maxAttempts(Integer.MAX_VALUE) - .includes(IOException.class) - .excludes(FileNotFoundException.class) - .build(); - - retryTemplate.setRetryPolicy(retryPolicy); - assertThat(invocationCount).hasValue(0); assertThatExceptionOfType(RetryException.class) .isThrownBy(() -> retryTemplate.execute(retryable))