@Target(value={ANNOTATION_TYPE,METHOD})
@Retention(value=RUNTIME)
@Documented
@API(status=STABLE,
since="5.7")
@TestTemplate
@ExtendWith(value=ParameterizedTestExtension.class)
public @interface ParameterizedTest
@ParameterizedTest is used to signal that the annotated method is a
parameterized test method.
Such methods must not be private or static.
@ParameterizedTest methods must specify at least one
ArgumentsProvider
via @ArgumentsSource
or a corresponding composed annotation (e.g., @ValueSource,
@CsvSource, etc.). The provider is responsible for providing a
Stream of
Arguments that will be
used to invoke the parameterized test method.
A @ParameterizedTest method may declare additional parameters at
the end of the method's parameter list to be resolved by other
ParameterResolvers
(e.g., TestInfo, TestReporter, etc). Specifically, a
parameterized test method must declare formal parameters according to the
following rules.
ParameterResolver
implementations must be declared last.In this context, an indexed argument is an argument for a given
index in the Arguments provided by an ArgumentsProvider that
is passed as an argument to the parameterized method at the same index in the
method's formal parameter list. An aggregator is any parameter of type
ArgumentsAccessor
or any parameter annotated with
@AggregateWith.
Method parameters may be annotated with
@ConvertWith
or a corresponding composed annotation to specify an explicit
ArgumentConverter.
Otherwise, JUnit Jupiter will attempt to perform an implicit
conversion to the target type automatically (see the User Guide for further
details).
@ParameterizedTest may also be used as a meta-annotation in order
to create a custom composed annotation that inherits the semantics
of @ParameterizedTest.
By default, test methods will be ordered using an algorithm that is
deterministic but intentionally nonobvious. This ensures that subsequent runs
of a test suite execute test methods in the same order, thereby allowing for
repeatable builds. In this context, a test method is any instance
method that is directly annotated or meta-annotated with @Test,
@RepeatedTest, @ParameterizedTest, @TestFactory, or
@TestTemplate.
Although true unit tests typically should not rely on the order
in which they are executed, there are times when it is necessary to enforce
a specific test method execution order — for example, when writing
integration tests or functional tests where the sequence of
the tests is important, especially in conjunction with
@TestInstance(Lifecycle.PER_CLASS).
To control the order in which test methods are executed, annotate your
test class or test interface with
@TestMethodOrder and specify
the desired MethodOrderer
implementation.
Arguments,
ArgumentsProvider,
ArgumentsSource,
CsvFileSource,
CsvSource,
EnumSource,
MethodSource,
ValueSource,
ArgumentsAccessor,
AggregateWith,
ArgumentConverter,
ConvertWith| Modifier and Type | Fields and Description |
|---|---|
static java.lang.String |
ARGUMENTS_PLACEHOLDER
Placeholder for the complete, comma-separated arguments list of the
current invocation of a
@ParameterizedTest method:
{arguments} |
static java.lang.String |
ARGUMENTS_WITH_NAMES_PLACEHOLDER
Placeholder for the complete, comma-separated named arguments list
of the current invocation of a
@ParameterizedTest method:
{argumentsWithNames} |
static java.lang.String |
DEFAULT_DISPLAY_NAME
Default display name pattern for the current invocation of a
@ParameterizedTest method: "[{index}] {argumentsWithNames}" |
static java.lang.String |
DISPLAY_NAME_PLACEHOLDER
|
static java.lang.String |
INDEX_PLACEHOLDER
Placeholder for the current invocation index of a
@ParameterizedTest
method (1-based): {index} |
| Modifier and Type | Optional Element and Description |
|---|---|
boolean |
autoCloseArguments
Configure whether all arguments of the parameterized test that implement
AutoCloseable
will be closed after @AfterEach methods
and AfterEachCallback
extensions have been called for the current parameterized test invocation. |
java.lang.String |
name
The display name to be used for individual invocations of the
parameterized test; never blank or consisting solely of whitespace.
|
public static final java.lang.String DISPLAY_NAME_PLACEHOLDER
name()public static final java.lang.String INDEX_PLACEHOLDER
@ParameterizedTest
method (1-based): {index}name()public static final java.lang.String ARGUMENTS_PLACEHOLDER
@ParameterizedTest method:
{arguments}name()public static final java.lang.String ARGUMENTS_WITH_NAMES_PLACEHOLDER
@ParameterizedTest method:
{argumentsWithNames}
Argument names will be retrieved via the Parameter.getName()
API if the byte code contains parameter names — for example, if
the code was compiled with the -parameters command line argument
for javac.
name()public static final java.lang.String DEFAULT_DISPLAY_NAME
@ParameterizedTest method: "[{index}] {argumentsWithNames}"
Note that the default pattern does not include the
display name of the
@ParameterizedTest method.
name(),
DISPLAY_NAME_PLACEHOLDER,
INDEX_PLACEHOLDER,
ARGUMENTS_WITH_NAMES_PLACEHOLDERpublic abstract java.lang.String name
Defaults to {default_display_name}.
If the default display name flag ({default_display_name})
is not overridden, JUnit will:
Launcher API, build tools (e.g.,
Gradle and Maven), a JVM system property, or the JUnit Platform configuration
file (i.e., a file named junit-platform.properties in the root of
the class path). Consult the User Guide for further information.DEFAULT_DISPLAY_NAME constant will
be used.DISPLAY_NAME_PLACEHOLDERINDEX_PLACEHOLDERARGUMENTS_PLACEHOLDERARGUMENTS_WITH_NAMES_PLACEHOLDER{0}, {1}, etc.: an individual argument (0-based)For the latter, you may use MessageFormat patterns
to customize formatting. Please note that the original arguments are
passed when formatting, regardless of any implicit or explicit argument
conversions.
Note that {default_display_name} is a flag rather than a
placeholder.
MessageFormat@API(status=STABLE,
since="5.10")
public abstract boolean autoCloseArguments
AutoCloseable
will be closed after @AfterEach methods
and AfterEachCallback
extensions have been called for the current parameterized test invocation.
Defaults to true.
WARNING: if an argument that implements AutoCloseable
is reused for multiple invocations of the same parameterized test method,
you must set autoCloseArguments to false to ensure that
the argument is not closed between invocations.
AutoCloseable