Annotation Type TestParameters


  • @Retention(RUNTIME)
    @Target({CONSTRUCTOR,METHOD})
    @Repeatable(RepeatedTestParameters.class)
    public @interface TestParameters
    Annotation that can be placed (repeatedly) 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 still use other types of parameterization, such as @TestParameter.

    See value() for simple examples.

    Warning: This annotation can only be used if the compiled java code contains the parameter names. This is typically done by passing the -parameters option to the Java compiler, which requires using Java 8 or higher and may not be available on Android.

    • Element Detail

      • value

        String[] value
        Specifies one or more stringified sets of parameters in YAML format. Each set 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 and the section below on the supported parameter types.

        There are two distinct ways of using this annotation: repeated vs single:

        Recommended usage: Separate annotation per parameter set

        This approach uses multiple @TestParameters annotations, one for each set of parameters, for example:

         @Test
         @TestParameters("{age: 17, expectIsAdult: false}")
         @TestParameters("{age: 22, expectIsAdult: true}")
         public void personIsAdult(int age, boolean expectIsAdult) { ... }
        
         @Test
         @TestParameters("{updateRequest: {country_code: BE}, expectedResultType: SUCCESS}")
         @TestParameters("{updateRequest: {country_code: XYZ}, expectedResultType: FAILURE}")
         public void update(UpdateRequest updateRequest, ResultType expectedResultType) { ... }
         

        Old discouraged usage: Single annotation with all parameter sets

        This approach uses a single @TestParameter annotation for all parameter sets, for example:

         @Test
         @TestParameters({
           "{age: 17, expectIsAdult: false}",
           "{age: 22, expectIsAdult: true}",
         })
         public void personIsAdult(int age, boolean expectIsAdult) { ... }
        
         @Test
         @TestParameters({
           "{updateRequest: {country_code: BE}, expectedResultType: SUCCESS}",
           "{updateRequest: {country_code: XYZ}, expectedResultType: FAILURE}",
         })
         public void update(UpdateRequest updateRequest, ResultType expectedResultType) { ... }
         

        Supported parameter types

        • 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.

        Default:
        {}
      • customName

        String customName
        Overrides the name of the parameter set that is used in the test name.

        This can only be set if value() has exactly one element. If not set, the YAML string in value() is used in the test name.

        For example: If this name is set to "young adult", then the test name might be "personIsAdult[young adult]" where the default might have been "personIsAdult[{age: 17, expectIsAdult: false}]".

        Default:
        ""
      • valuesProvider

        Class<? extends TestParameters.TestParametersValuesProvider> 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