Annotation Type AfterEach
-
@Target({ANNOTATION_TYPE,METHOD}) @Retention(RUNTIME) @Documented @API(status=STABLE, since="5.0") public @interface AfterEach@AfterEachis used to signal that the annotated method should be executed after each@Test,@RepeatedTest,@ParameterizedTest,@TestFactory, and@TestTemplatemethod in the current test class.Method Signatures
@AfterEachmethods must have avoidreturn type, must not beprivate, and must not bestatic. They may optionally declare parameters to be resolved byParameterResolvers.Inheritance and Execution Order
@AfterEachmethods are inherited from superclasses as long as they are not overridden. Furthermore,@AfterEachmethods from superclasses will be executed after@AfterEachmethods in subclasses.Similarly,
@AfterEachmethods declared as interface default methods are inherited as long as they are not overridden, and@AfterEachdefault methods will be executed after@AfterEachmethods in the class that implements the interface.JUnit Jupiter does not guarantee the execution order of multiple
@AfterEachmethods 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,
@AfterEachmethods are in no way linked to@BeforeEachmethods. Consequently, there are no guarantees with regard to their wrapping behavior. For example, given two@BeforeEachmethodscreateA()andcreateB()as well as two@AfterEachmethodsdestroyA()anddestroyB(), the order in which the@BeforeEachmethods are executed (e.g.createA()beforecreateB()) does not imply any order for the seemingly corresponding@AfterEachmethods. In other words,destroyA()might be called before or afterdestroyB(). The JUnit Team therefore recommends that developers declare at most one@BeforeEachmethod and at most one@AfterEachmethod per test class or test interface unless there are no dependencies between the@BeforeEachmethods or between the@AfterEachmethods.Composition
@AfterEachmay be used as a meta-annotation in order to create a custom composed annotation that inherits the semantics of@AfterEach.- Since:
- 5.0
- See Also:
BeforeEach,BeforeAll,AfterAll,Test,RepeatedTest,TestFactory,TestTemplate