Verifying retries with @Retryable
About
In Spring Boot applications, the @Retryable annotation (from Spring Retry) allows methods to be retried automatically upon specified exceptions. This is especially useful for transient failures such as network issues, service timeouts, or temporary unavailability of external systems.
During integration testing, it’s often necessary to verify that the retry mechanism works as expected—specifically, that the method is invoked the correct number of times before ultimately failing or succeeding.
Example
Actual Source Code
Enable Spring Retry in our main class or any configuration class.
@SpringBootApplication
@EnableRetry
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}Create a Service with @Retryable
@Service
public class PaymentService {
@Retryable(value = SomeRetryableException.class, maxAttempts = 3)
public void abc() {
throw new SomeRetryableException("Simulated failure");
}
}Use the Service in a Controller
Test Class
Write Integration Test to Verify Retries
@SpyBeancreates a proxy of the real Spring bean and allows us to verify method invocations.
When the controller is triggered,
paymentService.abc()is called.The method always throws
SomeRetryableException, which triggers the retry logic.Spring Retry attempts the method 3 times (
maxAttempts = 3).@SpyBeanis used to spy on the realPaymentServicebean.Mockito.verify(..., times(3))asserts that the method was retried exactly 3 times.
Last updated