Annotation Type TestParameter


  • @Retention(RUNTIME)
    @Target({FIELD,PARAMETER})
    public @interface TestParameter
    Test parameter annotation that defines the values that a single parameter can have.

    For enums and booleans, the values can be automatically derived as all possible values:

     @Test
     public void test1(@TestParameter MyEnum myEnum, @TestParameter boolean myBoolean) {
       // ... will run for [(A,false), (A,true), (B,false), (B,true), (C,false), (C,true)]
     }
    
     enum MyEnum { A, B, C }
     

    The values can be explicitly defined as a parsed string:

     public void test1(
         @TestParameter({"{name: Hermione, age: 18}", "{name: Dumbledore, age: 115}"})
             UpdateCharacterRequest request,
         @TestParameter({"1", "4"}) int bookNumber) {
       // ... will run for [(Hermione,1), (Hermione,4), (Dumbledore,1), (Dumbledore,4)]
     }
     

    For more flexibility, see {valuesProvider()}. If you don't want to test all possible combinations but instead want to specify sets of parameters explicitly, use @TestParameters.

    • Element Detail

      • value

        String[] value
        Array of stringified values for the annotated type.

        Types that are supported:

        • String: No parsing happens
        • boolean: Specified as YAML boolean
        • long and int: Specified as YAML integer
        • float and double: Specified as YAML floating point or integer
        • 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.

        For examples, see TestParameter.

        Default:
        {}
      • valuesProvider

        Class<? extends TestParameter.TestParameterValuesProvider> valuesProvider
        Sets a provider that will return a list of parameter values.

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

        Example

         import com.google.testing.junit.testparameterinjector.TestParameterValuesProvider;
        
         @Test
         public void matchesAllOf_throwsOnNull(
             @TestParameter(valuesProvider = CharMatcherProvider.class)
                 CharMatcher charMatcher) {
           assertThrows(NullPointerException.class, () -> charMatcher.matchesAllOf(null));
         }
        
         private static final class CharMatcherProvider extends TestParameterValuesProvider {
           @Override
           public List<CharMatcher> provideValues(Context context) {
             return ImmutableList.of(CharMatcher.any(), CharMatcher.ascii(), CharMatcher.whitespace());
           }
         }
         
        Default:
        com.google.testing.junit.testparameterinjector.TestParameter.DefaultTestParameterValuesProvider.class