Annotation 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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classDefaultTestParameter.TestParameterValuesProviderimplementation that does nothing.static interfaceDeprecated. -
Optional Element Summary
Optional ElementsModifier and TypeOptional ElementDescriptionString[]Array of stringified values for the annotated type.Class<? extends TestParameter.TestParameterValuesProvider>Sets a provider that will return a list of parameter values.
-
Element Details
-
value
String[] valueArray of stringified values for the annotated type.Types that are supported:
- String: No parsing happens, except that
"null"parses as null - 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(), or"null"for null - 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:
- {}
- String: No parsing happens, except that
-
valuesProvider
Class<? extends TestParameter.TestParameterValuesProvider> valuesProviderSets 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
-
TestParameterValuesProviderinstead.