Annotation Type TestParameters


@Retention(RUNTIME)
@Target({CONSTRUCTOR,METHOD})
public @interface TestParameters
Annotation that can be placed on @Test-methods or a test constructor to indicate the sets of parameters that it should be invoked with.

For @Test-methods, the method will be invoked for every set of parameters that is specified. For constructors, all the tests in the test class will be invoked on a class instance that was constructed by each set of parameters.

Note: If this annotation is used in a test class, the other methods in that class can use other types of parameterization, such as @TestParameter.

See value() for simple examples.

  • Element Details

    • value

      String[] value
      Array of stringified set of parameters in YAML format. Each element corresponds to a single invocation of a test method.

      Each element in this array is a full parameter set, formatted as a YAML mapping. The mapping keys must match the parameter names and the mapping values will be converted to the parameter type if possible. See yaml.org for the YAML syntax. Parameter types that are supported:

      • YAML primitives:
        • String: Specified as YAML string
        • boolean: Specified as YAML boolean
        • long and int: Specified as YAML integer
        • float and double: Specified as YAML floating point or integer
      • Parsed types:
        • Enum value: Specified as a String that can be parsed by Enum.valueOf()
        • Byte array or com.google.protobuf.ByteString: Specified as an UTF8 String or YAML bytes (example: "!!binary 'ZGF0YQ=='")

      For dynamic sets of parameters or parameter types that are not supported here, use valuesProvider() and leave this field empty.

      Examples

       @Test
       @TestParameters({
         "{age: 17, expectIsAdult: false}",
         "{age: 22, expectIsAdult: true}",
       })
       public void personIsAdult(int age, boolean expectIsAdult) { ... }
      
       @Test
       @TestParameters({
         "{updateRequest: {name: 'Hermione'}, expectedResultType: SUCCESS}",
         "{updateRequest: {name: '---'}, expectedResultType: FAILURE}",
       })
       public void update(UpdateRequest updateRequest, ResultType expectedResultType) { ... }
       
      Default:
      {}
    • valuesProvider

      Sets a provider that will return a list of parameter sets. Each element in the returned list corresponds to a single invocation of a test method.

      If this field is set, value() must be empty and vice versa.

      Example

       @Test
       @TestParameters(valuesProvider = IsAdultValueProvider.class)
       public void personIsAdult(int age, boolean expectIsAdult) { ... }
      
       private static final class IsAdultValueProvider implements TestParametersValuesProvider {
         @Override public List<TestParametersValues> provideValues() {
           return ImmutableList.of(
             TestParametersValues.builder()
               .name("teenager")
               .addParameter("age", 17)
               .addParameter("expectIsAdult", false)
               .build(),
             TestParametersValues.builder()
               .name("young adult")
               .addParameter("age", 22)
               .addParameter("expectIsAdult", true)
               .build()
           );
         }
       }
       
      Default:
      com.google.testing.junit.testparameterinjector.TestParameters.DefaultTestParametersValuesProvider.class