Annotation Interface BeforeParameterizedClassInvocation
@BeforeParameterizedClassInvocation
is used to signal that the
annotated method should be executed before each
invocation of the current @ParameterizedClass
.
Declaring @BeforeParameterizedClassInvocation
methods in a
regular, non-parameterized test class has no effect and will be ignored.
Method Signatures
@BeforeParameterizedClassInvocation
methods must have a
void
return type, must not be private, and must be static
by
default. Consequently, @BeforeParameterizedClassInvocation
methods
are not supported in @Nested
test
classes or as interface default methods unless the test class is
annotated with
@TestInstance(Lifecycle.PER_CLASS)
.
However, beginning with Java 16 @BeforeParameterizedClassInvocation
methods may be declared as static
in
@Nested
test classes, in which case the
Lifecycle.PER_CLASS
restriction no longer applies.
Method Arguments
@BeforeParameterizedClassInvocation
methods may optionally declare
parameters that are resolved depending on the setting of the
injectArguments()
attribute.
If injectArguments()
is set to false
, the parameters must
be resolved by other registered
ParameterResolvers
.
If injectArguments()
is set to true
(the default), the
method must declare the same parameters, in the same order, as the
indexed parameters (see
@ParameterizedClass
) of the parameterized test
class. It may declare a subset of the indexed parameters starting from the
first argument. Additionally, the method may declare custom aggregator
parameters (see @ParameterizedClass
) at the
end of its parameter list. If the method declares additional parameters after
these aggregator parameters, or more parameters than the class has indexed
parameters, they may be resolved by other
ParameterResolvers
.
For example, given a @ParameterizedClass
with
indexed parameters of type int
and String
, the
following method signatures are valid:
@BeforeParameterizedClassInvocation
void beforeInvocation() { ... }
@BeforeParameterizedClassInvocation
void beforeInvocation(int number) { ... }
@BeforeParameterizedClassInvocation
void beforeInvocation(int number, String text) { ... }
@BeforeParameterizedClassInvocation
void beforeInvocation(int number, String text, TestInfo testInfo) { ... }
@BeforeParameterizedClassInvocation
void beforeInvocation(ArgumentsAccessor accessor) { ... }
@BeforeParameterizedClassInvocation
void beforeInvocation(ArgumentsAccessor accessor, TestInfo testInfo) { ... }
@BeforeParameterizedClassInvocation
void beforeInvocation(int number, String text, ArgumentsAccessor accessor) { ... }
@BeforeParameterizedClassInvocation
void beforeInvocation(int number, String text, ArgumentsAccessor accessor, TestInfo testInfo) { ... }
In the snippet above,ArgumentsAccessor
is used as an example of an
aggregator parameter but the same applies to any parameter annotated with
@AggregateWith
. The parameter of type
TestInfo
is used as an example of a
parameter that is resolved by another
ParameterResolver
.
Inheritance and Execution Order
@BeforeParameterizedClassInvocation
methods are inherited from
superclasses as long as they are not overridden according to the
visibility rules of the Java language. Furthermore,
@BeforeParameterizedClassInvocation
methods from superclasses will be
executed before @BeforeParameterizedClassInvocation
methods in
subclasses.
Similarly, @BeforeParameterizedClassInvocation
methods declared in
an interface are inherited as long as they are not overridden, and
@BeforeParameterizedClassInvocation
methods from an interface will be
executed before @BeforeParameterizedClassInvocation
methods in the
class that implements the interface.
JUnit Jupiter does not guarantee the execution order of multiple
@BeforeParameterizedClassInvocation
methods that are declared within
a single parameterized test class or test interface. While it may at times
appear that these methods are invoked in alphabetical order, they are in fact
sorted using an algorithm that is deterministic but intentionally
non-obvious.
In addition, @BeforeParameterizedClassInvocation
methods are in no
way linked to @AfterParameterizedClassInvocation
methods.
Consequently, there are no guarantees with regard to their wrapping
behavior. For example, given two @BeforeParameterizedClassInvocation
methods createA()
and createB()
as well as two
@AfterParameterizedClassInvocation
methods destroyA()
and
destroyB()
, the order in which the
@BeforeParameterizedClassInvocation
methods are executed (e.g.
createA()
before createB()
) does not imply any order for the
seemingly corresponding @AfterParameterizedClassInvocation
methods.
In other words, destroyA()
might be called before or after
destroyB()
. The JUnit Team therefore recommends that developers
declare at most one @BeforeParameterizedClassInvocation
method and at
most one @AfterParameterizedClassInvocation
method per test class or
test interface unless there are no dependencies between the
@BeforeParameterizedClassInvocation
methods or between the
@AfterParameterizedClassInvocation
methods.
Composition
@BeforeParameterizedClassInvocation
may be used as a
meta-annotation in order to create a custom composed annotation that
inherits the semantics of @BeforeParameterizedClassInvocation
.
- Since:
- 5.13
- See Also:
-
Optional Element Summary
Optional ElementsModifier and TypeOptional ElementDescriptionboolean
Whether the arguments of the parameterized test class should be injected into the annotated method (defaults tofalse
).
-
Element Details
-
injectArguments
boolean injectArgumentsWhether the arguments of the parameterized test class should be injected into the annotated method (defaults tofalse
).- Default:
true
-