| 1 | /* |
| 2 | * Copyright 2015-2024 the original author or authors. |
| 3 | * |
| 4 | * All rights reserved. This program and the accompanying materials are |
| 5 | * made available under the terms of the Eclipse Public License v2.0 which |
| 6 | * accompanies this distribution and is available at |
| 7 | * |
| 8 | * https://www.eclipse.org/legal/epl-v20.html |
| 9 | */ |
| 10 | |
| 11 | package org.junit.jupiter.api; |
| 12 | |
| 13 | import static org.apiguardian.api.API.Status.STABLE; |
| 14 | |
| 15 | import java.lang.annotation.Documented; |
| 16 | import java.lang.annotation.ElementType; |
| 17 | import java.lang.annotation.Retention; |
| 18 | import java.lang.annotation.RetentionPolicy; |
| 19 | import java.lang.annotation.Target; |
| 20 | |
| 21 | import org.apiguardian.api.API; |
| 22 | |
| 23 | /** |
| 24 | * {@code @AfterEach} is used to signal that the annotated method should be |
| 25 | * executed <em>after</em> <strong>each</strong> {@code @Test}, |
| 26 | * {@code @RepeatedTest}, {@code @ParameterizedTest}, {@code @TestFactory}, |
| 27 | * and {@code @TestTemplate} method in the current test class. |
| 28 | * |
| 29 | * <h2>Method Signatures</h2> |
| 30 | * |
| 31 | * <p>{@code @AfterEach} methods must have a {@code void} return type and must |
| 32 | * not be {@code static}. Using {@code private} visibility is strongly |
| 33 | * discouraged and will be disallowed in a future release. |
| 34 | * They may optionally declare parameters to be resolved by |
| 35 | * {@link org.junit.jupiter.api.extension.ParameterResolver ParameterResolvers}. |
| 36 | * |
| 37 | * <h2>Inheritance and Execution Order</h2> |
| 38 | * |
| 39 | * <p>{@code @AfterEach} methods are inherited from superclasses as long as they |
| 40 | * are not <em>overridden</em> or <em>superseded</em> (i.e., replaced based on |
| 41 | * signature only, irrespective of Java's visibility rules). Furthermore, |
| 42 | * {@code @AfterEach} methods from superclasses will be executed after |
| 43 | * {@code @AfterEach} methods in subclasses. |
| 44 | * |
| 45 | * <p>Similarly, {@code @AfterEach} methods declared as <em>interface default |
| 46 | * methods</em> are inherited as long as they are not overridden, and |
| 47 | * {@code @AfterEach} default methods will be executed after {@code @AfterEach} |
| 48 | * methods in the class that implements the interface. |
| 49 | * |
| 50 | * <p>JUnit Jupiter does not guarantee the execution order of multiple |
| 51 | * {@code @AfterEach} methods that are declared within a single test class or |
| 52 | * test interface. While it may at times appear that these methods are invoked |
| 53 | * in alphabetical order, they are in fact sorted using an algorithm that is |
| 54 | * deterministic but intentionally non-obvious. |
| 55 | * |
| 56 | * <p>In addition, {@code @AfterEach} methods are in no way linked to |
| 57 | * {@code @BeforeEach} methods. Consequently, there are no guarantees with |
| 58 | * regard to their <em>wrapping</em> behavior. For example, given two |
| 59 | * {@code @BeforeEach} methods {@code createA()} and {@code createB()} as well |
| 60 | * as two {@code @AfterEach} methods {@code destroyA()} and {@code destroyB()}, |
| 61 | * the order in which the {@code @BeforeEach} methods are executed (e.g. |
| 62 | * {@code createA()} before {@code createB()}) does not imply any order for the |
| 63 | * seemingly corresponding {@code @AfterEach} methods. In other words, |
| 64 | * {@code destroyA()} might be called before <em>or</em> after |
| 65 | * {@code destroyB()}. The JUnit Team therefore recommends that developers |
| 66 | * declare at most one {@code @BeforeEach} method and at most one |
| 67 | * {@code @AfterEach} method per test class or test interface unless there are |
| 68 | * no dependencies between the {@code @BeforeEach} methods or between the |
| 69 | * {@code @AfterEach} methods. |
| 70 | * |
| 71 | * <h2>Composition</h2> |
| 72 | * |
| 73 | * <p>{@code @AfterEach} may be used as a meta-annotation in order to create |
| 74 | * a custom <em>composed annotation</em> that inherits the semantics of |
| 75 | * {@code @AfterEach}. |
| 76 | * |
| 77 | * @since 5.0 |
| 78 | * @see BeforeEach |
| 79 | * @see BeforeAll |
| 80 | * @see AfterAll |
| 81 | * @see Test |
| 82 | * @see RepeatedTest |
| 83 | * @see TestFactory |
| 84 | * @see TestTemplate |
| 85 | */ |
| 86 | @Target({ ElementType.ANNOTATION_TYPE, ElementType.METHOD }) |
| 87 | @Retention(RetentionPolicy.RUNTIME) |
| 88 | @Documented |
| 89 | @API(status = STABLE, since = "5.0") |
| 90 | public @interface AfterEach { |
| 91 | } |