Annotation Type ParameterizedTest
-
@Target({ANNOTATION_TYPE,METHOD}) @Retention(RUNTIME) @Documented @API(status=STABLE, since="5.7") @TestTemplate @ExtendWith(ParameterizedTestExtension.class) public @interface ParameterizedTest@ParameterizedTestis used to signal that the annotated method is a parameterized test method.Such methods must not be
privateorstatic.Argument Providers and Sources
@ParameterizedTestmethods must specify at least oneArgumentsProvidervia@ArgumentsSourceor a corresponding composed annotation (e.g.,@ValueSource,@CsvSource, etc.). The provider is responsible for providing aStreamofArgumentsthat will be used to invoke the parameterized test method.Formal Parameter List
A
@ParameterizedTestmethod may declare additional parameters at the end of the method's parameter list to be resolved by otherParameterResolvers(e.g.,TestInfo,TestReporter, etc). Specifically, a parameterized test method must declare formal parameters according to the following rules.- Zero or more indexed arguments must be declared first.
- Zero or more aggregators must be declared next.
- Zero or more arguments supplied by other
ParameterResolverimplementations must be declared last.
In this context, an indexed argument is an argument for a given index in the
Argumentsprovided by anArgumentsProviderthat 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 typeArgumentsAccessoror any parameter annotated with@AggregateWith.Argument Conversion
Method parameters may be annotated with
@ConvertWithor a corresponding composed annotation to specify an explicitArgumentConverter. Otherwise, JUnit Jupiter will attempt to perform an implicit conversion to the target type automatically (see the User Guide for further details).Composed Annotations
@ParameterizedTestmay also be used as a meta-annotation in order to create a custom composed annotation that inherits the semantics of@ParameterizedTest.Test Execution Order
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
@TestMethodOrderand specify the desiredMethodOrdererimplementation.- Since:
- 5.0
- See Also:
Arguments,ArgumentsProvider,ArgumentsSource,CsvFileSource,CsvSource,EnumSource,MethodSource,ValueSource,ArgumentsAccessor,AggregateWith,ArgumentConverter,ConvertWith
-
-
Field Summary
Fields Modifier and Type Fields Description static java.lang.StringARGUMENTS_PLACEHOLDERPlaceholder for the complete, comma-separated arguments list of the current invocation of a@ParameterizedTestmethod:{arguments}static java.lang.StringARGUMENTS_WITH_NAMES_PLACEHOLDERPlaceholder for the complete, comma-separated named arguments list of the current invocation of a@ParameterizedTestmethod:{argumentsWithNames}static java.lang.StringDEFAULT_DISPLAY_NAMEDefault display name pattern for the current invocation of a@ParameterizedTestmethod: "[{index}] {argumentsWithNames}"static java.lang.StringDISPLAY_NAME_PLACEHOLDERPlaceholder for the display name of a@ParameterizedTestmethod:{displayName}static java.lang.StringINDEX_PLACEHOLDERPlaceholder for the current invocation index of a@ParameterizedTestmethod (1-based):{index}
-
Optional Element Summary
Optional Elements Modifier and Type Optional Element Description booleanautoCloseArgumentsConfigure whether all arguments of the parameterized test that implementAutoCloseablewill be closed after@AfterEachmethods andAfterEachCallbackextensions have been called for the current parameterized test invocation.java.lang.StringnameThe display name to be used for individual invocations of the parameterized test; never blank or consisting solely of whitespace.
-
-
-
Field Detail
-
DISPLAY_NAME_PLACEHOLDER
static final java.lang.String DISPLAY_NAME_PLACEHOLDER
Placeholder for the display name of a@ParameterizedTestmethod:{displayName}- Since:
- 5.3
- See Also:
name()
-
-
-
INDEX_PLACEHOLDER
static final java.lang.String INDEX_PLACEHOLDER
Placeholder for the current invocation index of a@ParameterizedTestmethod (1-based):{index}- Since:
- 5.3
- See Also:
name()
-
-
-
ARGUMENTS_PLACEHOLDER
static final java.lang.String ARGUMENTS_PLACEHOLDER
Placeholder for the complete, comma-separated arguments list of the current invocation of a@ParameterizedTestmethod:{arguments}- Since:
- 5.3
- See Also:
name()
-
-
-
ARGUMENTS_WITH_NAMES_PLACEHOLDER
static final java.lang.String ARGUMENTS_WITH_NAMES_PLACEHOLDER
Placeholder for the complete, comma-separated named arguments list of the current invocation of a@ParameterizedTestmethod:{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-parameterscommand line argument forjavac.- Since:
- 5.6
- See Also:
name()
-
-
-
DEFAULT_DISPLAY_NAME
static final java.lang.String DEFAULT_DISPLAY_NAME
Default display name pattern for the current invocation of a@ParameterizedTestmethod: "[{index}] {argumentsWithNames}"Note that the default pattern does not include the display name of the
@ParameterizedTestmethod.- Since:
- 5.3
- See Also:
name(),DISPLAY_NAME_PLACEHOLDER,INDEX_PLACEHOLDER,ARGUMENTS_WITH_NAMES_PLACEHOLDER
-
-
Element Detail
-
name
java.lang.String name
The display name to be used for individual invocations of the parameterized test; never blank or consisting solely of whitespace.Defaults to
{default_display_name}.If the default display name flag (
{default_display_name}) is not overridden, JUnit will:- Look up the "junit.jupiter.params.displayname.default"
configuration parameter and use it if available. The configuration
parameter can be supplied via the
LauncherAPI, build tools (e.g., Gradle and Maven), a JVM system property, or the JUnit Platform configuration file (i.e., a file namedjunit-platform.propertiesin the root of the class path). Consult the User Guide for further information. - Otherwise, the value of the
DEFAULT_DISPLAY_NAMEconstant will be used.
Supported placeholders
DISPLAY_NAME_PLACEHOLDERINDEX_PLACEHOLDERARGUMENTS_PLACEHOLDERARGUMENTS_WITH_NAMES_PLACEHOLDER{0},{1}, etc.: an individual argument (0-based)
For the latter, you may use
MessageFormatpatterns 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.- See Also:
MessageFormat
- Default:
- "{default_display_name}"
- Look up the "junit.jupiter.params.displayname.default"
configuration parameter and use it if available. The configuration
parameter can be supplied via the
-
-
-
autoCloseArguments
@API(status=STABLE, since="5.10") boolean autoCloseArgumentsConfigure whether all arguments of the parameterized test that implementAutoCloseablewill be closed after@AfterEachmethods andAfterEachCallbackextensions have been called for the current parameterized test invocation.Defaults to
true.WARNING: if an argument that implements
AutoCloseableis reused for multiple invocations of the same parameterized test method, you must setautoCloseArgumentstofalseto ensure that the argument is not closed between invocations.- Since:
- 5.8
- See Also:
AutoCloseable
- Default:
- true
-
-