Class RetryExtension
- All Implemented Interfaces:
Extension,TestExecutionExceptionHandler
RetryTest annotation.
This extension will execute a test method multiple times on failure
based on the parameters specified in the RetryTest annotation.
It also supports adding a delay between retries to allow external
systems or resources to stabilize.
The extension uses TestExecutionExceptionHandler to intercept
and handle exceptions thrown during test execution. If the test succeeds
in any of the retry attempts, it is marked as passed. Otherwise, the
last exception thrown is re-thrown to indicate failure.
Features:
- Retries a test upon failure, up to the specified maximum attempts.
- Optionally introduces a delay between retry attempts.
- Reports the failure count and exception details for each retry on the console.
Exceptions encountered during the retry process are carefully handled:
- If a retry is interrupted during the delay, the thread is re-interrupted,
and the
InterruptedExceptionis added as a suppressed exception. - The original or last exception encountered is re-thrown when retries are exhausted.
This extension operates on individual test methods and requires them
to be annotated with RetryTest to activate the retry logic.
Note: Runtime exceptions during retries, such as InvocationTargetException,
are unwrapped to reveal the underlying cause.
- Since:
- 1.0
- Author:
- Erik C. Thauvin, Guillaume Laforge
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidhandleTestExecutionException(ExtensionContext extensionContext, Throwable throwable) Handles test execution exceptions, allowing retry mechanisms for a test method annotated with@RetryTest.
-
Constructor Details
-
RetryExtension
public RetryExtension()
-
-
Method Details
-
handleTestExecutionException
public void handleTestExecutionException(ExtensionContext extensionContext, Throwable throwable) throws Throwable Handles test execution exceptions, allowing retry mechanisms for a test method annotated with@RetryTest. This method retries the test execution up to a specified number of attempts and introduces optional delays between retries.If the test is not annotated with
@RetryTest, the exception is rethrown immediately. If all retry attempts fail, the last exception is thrown.- Specified by:
handleTestExecutionExceptionin interfaceTestExecutionExceptionHandler- Parameters:
extensionContext- the context in which the current test is executed, providing information about the test method and instancethrowable- the exception thrown during the initial execution of the test- Throws:
Throwable- if the test exhausts all retry attempts or is not eligible for retry
-