| 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 @AfterAll} is used to signal that the annotated method should be |
| 25 | * executed <em>after</em> <strong>all</strong> tests in the current test class. |
| 26 | * |
| 27 | * <p>In contrast to {@link AfterEach @AfterEach} methods, {@code @AfterAll} |
| 28 | * methods are only executed once for a given test class. |
| 29 | * |
| 30 | * <h2>Method Signatures</h2> |
| 31 | * |
| 32 | * <p>{@code @AfterAll} methods must have a {@code void} return type and must be |
| 33 | * {@code static} by default. Consequently, {@code @AfterAll} methods are not |
| 34 | * supported in {@link Nested @Nested} test classes or as <em>interface default |
| 35 | * methods</em> unless the test class is annotated with |
| 36 | * {@link TestInstance @TestInstance(Lifecycle.PER_CLASS)}. |
| 37 | * However, beginning with Java 16 {@code @AfterAll} methods may be declared as |
| 38 | * {@code static} in {@link Nested @Nested} test classes, and the |
| 39 | * {@code Lifecycle.PER_CLASS} restriction no longer applies. {@code @AfterAll} |
| 40 | * methods may optionally declare parameters to be resolved by |
| 41 | * {@link org.junit.jupiter.api.extension.ParameterResolver ParameterResolvers}. |
| 42 | * |
| 43 | * <p>Using {@code private} visibility for {@code @AfterAll} methods is |
| 44 | * strongly discouraged and will be disallowed in a future release. |
| 45 | * |
| 46 | * <h2>Inheritance and Execution Order</h2> |
| 47 | * |
| 48 | * <p>{@code @AfterAll} methods are inherited from superclasses as long as |
| 49 | * they are not <em>hidden</em> (default mode with {@code static} modifier), |
| 50 | * <em>overridden</em>, or <em>superseded</em> (i.e., replaced based on |
| 51 | * signature only, irrespective of Java's visibility rules). Furthermore, |
| 52 | * {@code @AfterAll} methods from superclasses will be executed before |
| 53 | * {@code @AfterAll} methods in subclasses. |
| 54 | * |
| 55 | * <p>Similarly, {@code @AfterAll} methods declared in an interface are |
| 56 | * inherited as long as they are not <em>hidden</em> or <em>overridden</em>, |
| 57 | * and {@code @AfterAll} methods from an interface will be executed after |
| 58 | * {@code @AfterAll} methods in the class that implements the interface. |
| 59 | * |
| 60 | * <p>JUnit Jupiter does not guarantee the execution order of multiple |
| 61 | * {@code @AfterAll} methods that are declared within a single test class or |
| 62 | * test interface. While it may at times appear that these methods are invoked |
| 63 | * in alphabetical order, they are in fact sorted using an algorithm that is |
| 64 | * deterministic but intentionally non-obvious. |
| 65 | * |
| 66 | * <p>In addition, {@code @AfterAll} methods are in no way linked to |
| 67 | * {@code @BeforeAll} methods. Consequently, there are no guarantees with regard |
| 68 | * to their <em>wrapping</em> behavior. For example, given two |
| 69 | * {@code @BeforeAll} methods {@code createA()} and {@code createB()} as well as |
| 70 | * two {@code @AfterAll} methods {@code destroyA()} and {@code destroyB()}, the |
| 71 | * order in which the {@code @BeforeAll} methods are executed (e.g. |
| 72 | * {@code createA()} before {@code createB()}) does not imply any order for the |
| 73 | * seemingly corresponding {@code @AfterAll} methods. In other words, |
| 74 | * {@code destroyA()} might be called before <em>or</em> after |
| 75 | * {@code destroyB()}. The JUnit Team therefore recommends that developers |
| 76 | * declare at most one {@code @BeforeAll} method and at most one |
| 77 | * {@code @AfterAll} method per test class or test interface unless there are no |
| 78 | * dependencies between the {@code @BeforeAll} methods or between the |
| 79 | * {@code @AfterAll} methods. |
| 80 | * |
| 81 | * <h2>Composition</h2> |
| 82 | * |
| 83 | * <p>{@code @AfterAll} may be used as a meta-annotation in order to create |
| 84 | * a custom <em>composed annotation</em> that inherits the semantics of |
| 85 | * {@code @AfterAll}. |
| 86 | * |
| 87 | * @since 5.0 |
| 88 | * @see BeforeAll |
| 89 | * @see BeforeEach |
| 90 | * @see AfterEach |
| 91 | * @see Test |
| 92 | * @see TestFactory |
| 93 | * @see TestInstance |
| 94 | */ |
| 95 | @Target({ ElementType.ANNOTATION_TYPE, ElementType.METHOD }) |
| 96 | @Retention(RetentionPolicy.RUNTIME) |
| 97 | @Documented |
| 98 | @API(status = STABLE, since = "5.0") |
| 99 | public @interface AfterAll { |
| 100 | } |