Skip to main content

@MethodAutoSource

The @MethodAutoSource annotation blends the capabilities of @MethodSource and @AutoSource. This allows you to specify the names of factory methods for the initial set of parameters, while the remaining parameters are automatically populated with arbitrary values.

How to Use

To employ @MethodAutoSource, annotate your test method with it and specify the factory method that provides the argument values for the initial parameters. AutoParams will generate values for the remaining parameters.

Here's a straightforward example:

@ParameterizedTest
@MethodAutoSource("factoryMethod")
void testMethod(int arg1, String arg2, String arg3) {
assertEquals(16, arg1);
assertEquals("foo", arg2);
assertNotEquals(arg2, arg3);
}

static Stream<Arguments> factoryMethod() {
return Stream.of(Arguments.of(16, "foo"));
}

In this instance, arg1 and arg2 are set based on the values provided by the factoryMethod. arg3 will be assigned a randomly generated value, and the test confirms that arg3 is not equal to arg2.

Compatibility with @Freeze

The @MethodAutoSource annotation is also fully compatible with the @Freeze annotation. This lets you lock in values for particular parameters while dynamically populating others through a factory method.

Here's how it works together:

@AllArgsConstructor
@Getter
public class ValueContainer {
private final String value;
}
@ParameterizedTest
@MethodAutoSource("factoryMethod")
void testMethod(int arg1, @Freeze String arg2, ValueContainer arg3) {
assertEquals("foo", arg3.getValue());
}

static Stream<Arguments> factoryMethod() {
return Stream.of(Arguments.of(16, "foo"));
}

Benefits

  1. Customizability: The ability to specify factory methods for some parameters allows for highly tailored test setups.
  2. Simplicity: With automatic value generation for remaining parameters, @MethodAutoSource alleviates the need for extensive manual data preparation.
  3. Flexibility: This feature allows you to test a wider range of scenarios by combining pre-defined and random values.

The @MethodAutoSource annotation enriches your testing toolkit, offering a balanced blend of control and randomness in your parameterized tests.