Annotation Interface RetryTest


JUnit annotation to mark a test method for retry on failure.

When a test fails, it will be retried up to the specified number of times. Optionally, a wait period can be specified between retry attempts.

By default, any exception triggers a retry. If withExceptions is specified, only matching exception types (or any exception in their cause chain) will trigger a retry — other failures fail fast without retrying.

This annotation automatically includes the RetryExtension, so no additional @ExtendWith annotation is required.

Usage examples:

 @RetryTest(3)
 void unstableTest() {
     // Test code that might fail intermittently
 }

 @RetryTest(value = 5, delay = 2)
 void testWithDelay() {
     // Test that waits 2 seconds between retry attempts
 }

 @RetryTest(value = 3, withExceptions = IOException.class)
 void testRetryOnIoException() {
     // Only retries if IOException is thrown (or is a cause)
 }

 @RetryTest(withExceptions = {SocketTimeoutException.class, ConnectException.class})
 void testRetryOnSpecificExceptions() {
     // Only retries on specific network exceptions
 }
Since:
1.0
Author:
Erik C. Thauvin
See Also:
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    int
    The number of seconds to wait between retry attempts.
    Optional name for the test template.
    int
    The maximum number of retry attempts for a failing test.
    Class<? extends Throwable>[]
    Exception types that should trigger a retry.
  • Element Details

    • delay

      int delay
      The number of seconds to wait between retry attempts.

      If set to 0 (default), no wait occurs between retries. This can be useful for tests that interact with external systems that may need time to recover or stabilize.

      Returns:
      the wait time in seconds between retry attempts
      Default:
      0
    • name

      String name
      Optional name for the test template. If not specified, a default name will be generated.
      Returns:
      the display name for the retry test template
      Default:
      ""
    • value

      int value
      The maximum number of retry attempts for a failing test.

      The test will be executed at most value() times after the initial failure.

      Returns:
      the number of retry attempts. Must be greater than 0
      Default:
      3
    • withExceptions

      Class<? extends Throwable>[] withExceptions
      Exception types that should trigger a retry.

      If empty, any exception triggers a retry. If specified, only matching exceptions (or their causes) will be retried.

      Default:
      {}