Annotation Interface AfterParameterizedClassInvocation


@Target({ANNOTATION_TYPE,METHOD}) @Retention(RUNTIME) @Documented @API(status=EXPERIMENTAL, since="5.13") public @interface AfterParameterizedClassInvocation
@AfterParameterizedClassInvocation is used to signal that the annotated method should be executed before each invocation of the current @ParameterizedClass.

Declaring @AfterParameterizedClassInvocation methods in a regular, non-parameterized test class has no effect and will be ignored.

Method Signatures

@AfterParameterizedClassInvocation methods must have a void return type, must not be private, and must be static by default. Consequently, @AfterParameterizedClassInvocation 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 @AfterParameterizedClassInvocation methods may be declared as static in @Nested test classes, in which case the Lifecycle.PER_CLASS restriction no longer applies.

Method Arguments

@AfterParameterizedClassInvocation 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:


 @AfterParameterizedClassInvocation
 void afterInvocation() { ... }

 @AfterParameterizedClassInvocation
 void afterInvocation(int number) { ... }

 @AfterParameterizedClassInvocation
 void afterInvocation(int number, String text) { ... }

 @AfterParameterizedClassInvocation
 void afterInvocation(int number, String text, TestInfo testInfo) { ... }

 @AfterParameterizedClassInvocation
 void afterInvocation(ArgumentsAccessor accessor) { ... }

 @AfterParameterizedClassInvocation
 void afterInvocation(ArgumentsAccessor accessor, TestInfo testInfo) { ... }

 @AfterParameterizedClassInvocation
 void afterInvocation(int number, String text, ArgumentsAccessor accessor) { ... }

 @AfterParameterizedClassInvocation
 void afterInvocation(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

@AfterParameterizedClassInvocation methods are inherited from superclasses as long as they are not overridden according to the visibility rules of the Java language. Furthermore, @AfterParameterizedClassInvocation methods from superclasses will be executed before @AfterParameterizedClassInvocation methods in subclasses.

Similarly, @AfterParameterizedClassInvocation methods declared in an interface are inherited as long as they are not overridden, and @AfterParameterizedClassInvocation methods from an interface will be executed before @AfterParameterizedClassInvocation methods in the class that implements the interface.

JUnit Jupiter does not guarantee the execution order of multiple @AfterParameterizedClassInvocation 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, @AfterParameterizedClassInvocation methods are in no way linked to @BeforeParameterizedClassInvocation methods. Consequently, there are no guarantees with regard to their wrapping behavior. For example, given two @AfterParameterizedClassInvocation methods createA() and createB() as well as two @BeforeParameterizedClassInvocation methods destroyA() and destroyB(), the order in which the @AfterParameterizedClassInvocation methods are executed (e.g. createA() before createB()) does not imply any order for the seemingly corresponding @BeforeParameterizedClassInvocation methods. In other words, destroyA() might be called before or after destroyB(). The JUnit Team therefore recommends that developers declare at most one @AfterParameterizedClassInvocation method and at most one @BeforeParameterizedClassInvocation method per test class or test interface unless there are no dependencies between the @AfterParameterizedClassInvocation methods or between the @BeforeParameterizedClassInvocation methods.

Composition

@AfterParameterizedClassInvocation may be used as a meta-annotation in order to create a custom composed annotation that inherits the semantics of @AfterParameterizedClassInvocation.

Since:
5.13
See Also:
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    boolean
    Whether the arguments of the parameterized test class should be injected into the annotated method (defaults to true).
  • Element Details

    • injectArguments

      boolean injectArguments
      Whether the arguments of the parameterized test class should be injected into the annotated method (defaults to true).
      Default:
      true