@Target(value={ANNOTATION_TYPE,METHOD}) @Retention(value=RUNTIME) @Documented @API(status=EXPERIMENTAL, since="5.0") @ArgumentsSource(value=org.junit.jupiter.params.provider.MethodArgumentsProvider.class) public @interface MethodSource
@MethodSource
is an ArgumentsSource
which provides access
to values returned from factory methods of the class in
which this annotation is declared or from static factory methods in external
classes referenced by fully qualified method name.
Each factory method must generate 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
@ParameterizedTest
method. Generally speaking this
translates to a Stream
of Arguments
(i.e., Stream<Arguments>
); however, the actual concrete return type
can take on many forms. In this context, a "stream" is anything that JUnit
can reliably convert into a Stream
, such as
Stream
,
DoubleStream
,
LongStream
,
IntStream
,
Collection
,
Iterator
,
Iterable
, an array of objects, or an array of primitives. The
"arguments" within the stream can be supplied as an instance of
Arguments
, an array of objects (e.g., Object[]
), or a single
value if the parameterized test method accepts a single argument.
The following table displays compatible method signatures for parameterized test methods and their corresponding factory methods.
@ParameterizedTest method | Factory method |
---|---|
void test(int) | static int[] factory() |
void test(int) | static IntStream factory() |
void test(String) | static String[] factory() |
void test(String) | static List<String> factory() |
void test(String) | static Stream<String> factory() |
void test(String, int) | static Object[][] factory() |
void test(String, int) | static Stream<Object[]> factory() |
void test(String, int) | static Stream<Arguments> factory() |
Factory methods within the test class must be static
unless the
PER_CLASS
test instance lifecycle mode is used; whereas, factory methods in external
classes must always be static
. In any case, factory methods must not
declare any parameters.
Arguments
,
ArgumentsSource
,
ParameterizedTest
,
TestInstance
public abstract String[] value
Factory methods in external classes must be referenced by fully
qualified method name — for example,
com.example.StringsProviders#blankStrings
.
If no factory method names are declared, a method within the test class that has the same name as the test method will be used as the factory method by default.
For further information, see the class-level JavaDoc.
Copyright © 2018. All rights reserved.