Annotation Type AfterAll
-
@Target({ANNOTATION_TYPE,METHOD}) @Retention(RUNTIME) @Documented @API(status=STABLE, since="5.0") public @interface AfterAll@AfterAllis used to signal that the annotated method should be executed after all tests in the current test class.In contrast to
@AfterEachmethods,@AfterAllmethods are only executed once per execution of a given test class. If the test class is annotated with@ClassTemplate, the@AfterAllmethods are executed once after the last invocation of the class template. If a@Nestedtest class is declared in a@ClassTemplate, its@AfterAllmethods are called once per execution of the nested test class, namely, once per invocation of the outer class template.Method Signatures
@AfterAllmethods must have avoidreturn type and must bestaticby default. Consequently,@AfterAllmethods are not supported in@Nestedtest classes or as interface default methods unless the test class is annotated with@TestInstance(Lifecycle.PER_CLASS). However, beginning with Java 16@AfterAllmethods may be declared asstaticin@Nestedtest classes, in which case theLifecycle.PER_CLASSrestriction no longer applies. In addition,@AfterAllmethods may optionally declare parameters to be resolved byParameterResolvers.Using
privatevisibility for@AfterAllmethods is strongly discouraged and will be disallowed in a future release.Inheritance and Execution Order
@AfterAllmethods are inherited from superclasses as long as they are not overridden according to the visibility rules of the Java language. Furthermore,@AfterAllmethods from superclasses will be executed after@AfterAllmethods in subclasses.Similarly,
@AfterAllmethods declared in an interface are inherited as long as they are not overridden, and@AfterAllmethods from an interface will be executed after@AfterAllmethods in the class that implements the interface.JUnit Jupiter does not guarantee the execution order of multiple
@AfterAllmethods that are declared within a single 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,
@AfterAllmethods are in no way linked to@BeforeAllmethods. Consequently, there are no guarantees with regard to their wrapping behavior. For example, given two@BeforeAllmethodscreateA()andcreateB()as well as two@AfterAllmethodsdestroyA()anddestroyB(), the order in which the@BeforeAllmethods are executed (e.g.createA()beforecreateB()) does not imply any order for the seemingly corresponding@AfterAllmethods. In other words,destroyA()might be called before or afterdestroyB(). The JUnit Team therefore recommends that developers declare at most one@BeforeAllmethod and at most one@AfterAllmethod per test class or test interface unless there are no dependencies between the@BeforeAllmethods or between the@AfterAllmethods.Composition
@AfterAllmay be used as a meta-annotation in order to create a custom composed annotation that inherits the semantics of@AfterAll.- Since:
- 5.0
- See Also:
BeforeAll,BeforeEach,AfterEach,Test,TestFactory,TestInstance