Annotation Interface TypeGeneratorMethodSource


@TypeGeneratorMethodSource is an ArgumentsSource that provides access to values from a TypedGenerator returned by a factory method for parameterized tests.

This annotation allows you to use a method that returns a configured TypedGenerator instance for your parameterized tests. The method will be invoked to obtain the generator, and the specified number of values will be generated and passed to your test method.

The method referenced by this annotation must:

  • Be static if referenced from a different class
  • Return a TypedGenerator instance
  • Take no parameters

Example Usage

 
 @ParameterizedTest
 @TypeGeneratorMethodSource("createStringGenerator")
 void testWithCustomGenerator(String value) {
     assertNotNull(value);
 }
 
 static TypedGenerator<String> createStringGenerator() {
     return Generators.strings(5, 10);
 }
 
 @ParameterizedTest
 @TypeGeneratorMethodSource(value = "createIntegerGenerator", count = 5)
 void testWithMultipleIntegers(Integer value) {
     assertNotNull(value);
 }
 
 static TypedGenerator<Integer> createIntegerGenerator() {
     return Generators.integers(1, 100);
 }
 
 

You can also reference a method in another class:

 
 @ParameterizedTest
 @TypeGeneratorMethodSource("de.cuioss.test.MyGeneratorFactory#createGenerator")
 void testWithExternalGenerator(MyType value) {
     // test with value
 }
 
 
Since:
2.0
Author:
Oliver Wolff
See Also:
  • Required Element Summary

    Required Elements
    Modifier and Type
    Required Element
    Description
    The name of the method to invoke to get the TypedGenerator.
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    int
    The number of values to generate from the TypedGenerator.
  • Element Details

    • value

      The name of the method to invoke to get the TypedGenerator.

      This can be:

      • A method name in the test class (e.g., "createGenerator")
      • A fully qualified method name in an external class (e.g., "com.example.MyGeneratorFactory#createGenerator")
      Returns:
      the method name or reference
    • count

      int count
      The number of values to generate from the TypedGenerator.

      If set to a value less than 1, a default of 1 will be used.

      Returns:
      the number of values to generate
      Default:
      1