Skip to main content

@Repeat

The @Repeat annotation in AutoParams provides you with a straightforward way to repeat a unit test multiple times, each time with newly generated, arbitrary test data. This can be particularly useful when you want to ensure that a piece of code functions as expected across a variety of input conditions.

How to Use

To use the @Repeat feature, you'll need to attach the @Repeat annotation alongside the @AutoSource annotation on your parameterized test method. The repeat property of the @Repeat annotation takes an integer value, specifying the number of times the test should be repeated.

Here's an example:

@ParameterizedTest
@AutoSource
@Repeat(10)
void testMethod(int a, int b) {
// This test method will be executed ten times with different sets of 'a' and 'b' each time.
Calculator sut = new Calculator();
int actual = sut.add(a, b);
assertEquals(a + b, actual);
}

In the example above, the test method testMethod will run ten times. Each run will have a new set of randomly generated values for a and b, thanks to the @AutoSource annotation. The @Repeat(10) ensures that this cycle happens ten times, enhancing the test coverage.

Benefits

  1. Diverse Test Data: The @Repeat feature allows you to test your methods across a wider range of automatically generated input values.
  2. Reduced Manual Effort: There's no need to manually specify multiple sets of test data or create loops within your tests.
  3. Increased Test Robustness: Repeating tests multiple times with varying data can uncover edge cases and improve the reliability of your software.

By using the @Repeat feature, you can increase the comprehensiveness and reliability of your test suite with minimal additional effort.