Annotation Interface ParameterizedTest
@ParameterizedTest is used to signal that the annotated method is a
parameterized test method.
Such methods must not be private or static.
Arguments Providers and Sources
@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.
Formal Parameter List
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.
- Zero or more indexed parameters must be declared first.
- Zero or more aggregators must be declared next.
- Zero or more parameters supplied by other
ParameterResolverimplementations must be declared last.
In this context, an indexed parameter 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.
Argument Conversion
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).
Composed Annotations
@ParameterizedTest may also be used as a meta-annotation in order
to create a custom composed annotation that inherits the semantics
of @ParameterizedTest.
Inheritance
@ParameterizedTest methods are inherited from superclasses as long
as they are not overridden according to the visibility rules of the
Java language. Similarly, @ParameterizedTest methods declared as
interface default methods are inherited as long as they are not
overridden.
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
@TestMethodOrder and specify
the desired MethodOrderer
implementation.
- Since:
- 5.0
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringDeprecated.static final StringDeprecated.Please referenceParameterizedInvocationConstants.ARGUMENT_SET_NAME_PLACEHOLDERinstead.static final StringDeprecated.Please referenceParameterizedInvocationConstants.ARGUMENTS_PLACEHOLDERinstead.static final StringDeprecated.Please referenceParameterizedInvocationConstants.ARGUMENTS_WITH_NAMES_PLACEHOLDERinstead.static final StringDeprecated.Please referenceParameterizedInvocationConstants.DEFAULT_DISPLAY_NAMEinstead.static final StringDeprecated.Please referenceParameterizedInvocationConstants.DISPLAY_NAME_PLACEHOLDERinstead.static final StringDeprecated.Please referenceParameterizedInvocationConstants.INDEX_PLACEHOLDERinstead. -
Optional Element Summary
Optional ElementsModifier and TypeOptional ElementDescriptionbooleanConfigure whether zero invocations are allowed for this parameterized test.Configure how the number of arguments provided by anArgumentsSourceare validated.booleanConfigure whether all arguments of the parameterized test that implementAutoCloseablewill be closed after their corresponding invocation.The display name to be used for individual invocations of the parameterized test; never blank or consisting solely of whitespace.
-
Field Details
-
DISPLAY_NAME_PLACEHOLDER
Deprecated.Please referenceParameterizedInvocationConstants.DISPLAY_NAME_PLACEHOLDERinstead.- Since:
- 5.3
- See Also:
-
INDEX_PLACEHOLDER
Deprecated.Please referenceParameterizedInvocationConstants.INDEX_PLACEHOLDERinstead.- Since:
- 5.3
- See Also:
-
ARGUMENTS_PLACEHOLDER
Deprecated.Please referenceParameterizedInvocationConstants.ARGUMENTS_PLACEHOLDERinstead.- Since:
- 5.3
- See Also:
-
ARGUMENTS_WITH_NAMES_PLACEHOLDER
@API(status=DEPRECATED, since="5.13") @Deprecated static final String ARGUMENTS_WITH_NAMES_PLACEHOLDERDeprecated.Please referenceParameterizedInvocationConstants.ARGUMENTS_WITH_NAMES_PLACEHOLDERinstead.- Since:
- 5.6
- See Also:
-
ARGUMENT_SET_NAME_PLACEHOLDER
Deprecated.Please referenceParameterizedInvocationConstants.ARGUMENT_SET_NAME_PLACEHOLDERinstead.- Since:
- 5.11
- See Also:
-
ARGUMENT_SET_NAME_OR_ARGUMENTS_WITH_NAMES_PLACEHOLDER
@API(status=DEPRECATED, since="5.13") @Deprecated static final String ARGUMENT_SET_NAME_OR_ARGUMENTS_WITH_NAMES_PLACEHOLDERDeprecated.Please referenceParameterizedInvocationConstants.ARGUMENT_SET_NAME_OR_ARGUMENTS_WITH_NAMES_PLACEHOLDERinstead.- Since:
- 5.11
- See Also:
-
DEFAULT_DISPLAY_NAME
Deprecated.Please referenceParameterizedInvocationConstants.DEFAULT_DISPLAY_NAMEinstead.- Since:
- 5.3
- See Also:
-
-
Element Details
-
name
String nameThe 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,
"[{index}] {argumentSetNameOrArgumentsWithNames}"will be used.
Supported placeholders
"{displayName}""{index}""{argumentSetName}""{arguments}""{argumentsWithNames}""{argumentSetNameOrArgumentsWithNames}""{0}","{1}", etc.: an individual argument (0-based)
For the latter, you may use
MessageFormatpatterns to customize formatting (for example,{0,number,#.###}). 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:
- 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
Configure whether all arguments of the parameterized test that implementAutoCloseablewill be closed after their corresponding 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:
- Default:
true
-
allowZeroInvocations
Configure whether zero invocations are allowed for this parameterized test.Set this attribute to
trueif the absence of invocations is expected in some cases and should not cause a test failure.Defaults to
false.- Since:
- 5.12
- Default:
false
-
argumentCountValidation
Configure how the number of arguments provided by anArgumentsSourceare validated.Defaults to
ArgumentCountValidationMode.DEFAULT.When an
ArgumentsSourceprovides more arguments than declared by the parameterized test method, there might be a bug in the method or theArgumentsSource. By default, the additional arguments are ignored.argumentCountValidationallows you to control how additional arguments are handled. The default can be configured via the "junit.jupiter.params.argumentCountValidation" configuration parameter (see the User Guide for details on configuration parameters).- Since:
- 5.12
- See Also:
- Default:
DEFAULT
-
ParameterizedInvocationConstants.ARGUMENT_SET_NAME_OR_ARGUMENTS_WITH_NAMES_PLACEHOLDERinstead.