Annotation Type FieldSource
-
@Target({ANNOTATION_TYPE,METHOD,TYPE}) @Retention(RUNTIME) @Documented @Inherited @Repeatable(FieldSources.class) @API(status=MAINTAINED, since="5.13.3") @ArgumentsSource(FieldArgumentsProvider.class) public @interface FieldSource@FieldSourceis a repeatableArgumentsSourcewhich provides access to values of fields of the class in which this annotation is declared or from static fields in external classes referenced by fully qualified field name.Each field must be able to supply a stream of arguments, and each set of "arguments" within the "stream" will be provided as the physical arguments for individual invocations of the annotated
@ParameterizedClassor@ParameterizedTest.In this context, a "stream" is anything that JUnit can reliably convert to a
Stream; however, the actual concrete field type can take on many forms. Generally speaking this translates to aCollection, anIterable, aSupplierof a stream (Stream,DoubleStream,LongStream, orIntStream), aSupplierof anIterator, an array of objects or primitives, or any type that provides anIterator-returningiterator()method (such as, for example, akotlin.sequences.Sequence). Each set of "arguments" within the "stream" can be supplied as an instance ofArguments, an array of objects (for example,Object[],String[], etc.), or a single value if the parameterized class or test accepts a single argument.In contrast to the supported return types for
@MethodSourcefactory methods, the value of a@FieldSourcefield cannot be an instance ofStream,DoubleStream,LongStream,IntStream, orIterator, since the values of such types are consumed the first time they are processed. However, if you wish to use one of these types, you can wrap it in aSupplier— for example,Supplier<IntStream>.If the
Supplierreturn type isStreamor one of the primitive streams, JUnit will properly close it by callingBaseStream.close(), making it safe to use a resource such asFiles.lines().Please note that a one-dimensional array of objects supplied as a set of "arguments" will be handled differently than other types of arguments. Specifically, all of the elements of a one-dimensional array of objects will be passed as individual physical arguments to the
@ParameterizedTestmethod. This behavior can be seen in the table below for theSupplier<Stream<Object[]>> objectArrayStreamSupplierfield: the@ParameterizedTestmethod accepts individualStringandintarguments rather than a singleObject[]array. In contrast, any multidimensional array supplied as a set of "arguments" will be passed as a single physical argument to the@ParameterizedTestmethod without modification. This behavior can be seen in the table below for theSupplier<Stream<int[][]>> twoDimensionalIntArrayStreamSupplierandSupplier<Stream<Object[][]>> twoDimensionalObjectArrayStreamSupplierfields: the@ParameterizedTestmethods for those fields accept individualint[][]andObject[][]arguments, respectively.Examples
The following table displays compatible method signatures for parameterized test methods and their corresponding
@FieldSourcefields.Compatible method signatures and field declarations @ParameterizedTestmethod@FieldSourcefieldvoid test(String)static List<String> listOfStringsvoid test(String)static String[] arrayOfStringsvoid test(int)static int[] intArrayvoid test(int[])static int[][] twoDimensionalIntArrayvoid test(String, String)static String[][] twoDimensionalStringArrayvoid test(String, int)static Object[][] twoDimensionalObjectArrayvoid test(int)static Supplier<IntStream> intStreamSuppliervoid test(String)static Supplier<Stream<String>> stringStreamSuppliervoid test(String, int)static Supplier<Stream<Object[]>> objectArrayStreamSuppliervoid test(String, int)static Supplier<Stream<Arguments>> argumentsStreamSuppliervoid test(int[])static Supplier<Stream<int[]>> intArrayStreamSuppliervoid test(int[][])static Supplier<Stream<int[][]>> twoDimensionalIntArrayStreamSuppliervoid test(Object[][])static Supplier<Stream<Object[][]>> twoDimensionalObjectArrayStreamSupplierFields within the test class must be
staticunless thePER_CLASStest instance lifecycle mode is used; whereas, fields in external classes must always bestatic.This behavior and the above examples also apply to parameters of a
@ParameterizedClass, regardless whether field or constructor injection is used.Inheritance
This annotation is inherited to subclasses.
- Since:
- 5.11
- See Also:
MethodSource,Arguments,ArgumentsSource,ParameterizedClass,ParameterizedTest,TestInstance
-
-
Optional Element Summary
Optional Elements Modifier and Type Optional Element Description java.lang.String[]valueThe names of fields within the test class or in external classes to use as sources for arguments.
-
-
-
Element Detail
-
value
java.lang.String[] value
The names of fields within the test class or in external classes to use as sources for arguments.Fields in external classes must be referenced by fully qualified field name — for example,
"com.example.WebUtils#httpMethodNames"or"com.example.TopLevelClass$NestedClass#numbers"for a field in a static nested class.If no field names are declared, a field within the test class that has the same name as the test method will be used as the field by default in case this annotation is applied to a
@ParameterizedTestmethod. For a@ParameterizedClass, at least one field name must be declared explicitly.For further information, see the class-level Javadoc.
- Default:
- {}
-
-