| 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.extension; |
| 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.Inherited; |
| 18 | import java.lang.annotation.Repeatable; |
| 19 | import java.lang.annotation.Retention; |
| 20 | import java.lang.annotation.RetentionPolicy; |
| 21 | import java.lang.annotation.Target; |
| 22 | |
| 23 | import org.apiguardian.api.API; |
| 24 | |
| 25 | /** |
| 26 | * {@code @ExtendWith} is a {@linkplain Repeatable repeatable} annotation |
| 27 | * that is used to register {@linkplain Extension extensions} for the annotated |
| 28 | * test class, test interface, test method, parameter, or field. |
| 29 | * |
| 30 | * <p>Annotated parameters are supported in test class constructors, in test |
| 31 | * methods, and in {@code @BeforeAll}, {@code @AfterAll}, {@code @BeforeEach}, |
| 32 | * and {@code @AfterEach} lifecycle methods. |
| 33 | * |
| 34 | * <p>{@code @ExtendWith} fields may be either {@code static} or non-static. |
| 35 | * |
| 36 | * <h2>Inheritance</h2> |
| 37 | * |
| 38 | * <p>{@code @ExtendWith} fields are inherited from superclasses as long as they |
| 39 | * are not <em>hidden</em> or <em>overridden</em>. Furthermore, {@code @ExtendWith} |
| 40 | * fields from superclasses will be registered before {@code @ExtendWith} fields |
| 41 | * in subclasses. |
| 42 | * |
| 43 | * <h2>Registration Order</h2> |
| 44 | * |
| 45 | * <p>When {@code @ExtendWith} is present on a test class, test interface, or |
| 46 | * test method or on a parameter in a test method or lifecycle method, the |
| 47 | * corresponding extensions will be registered in the order in which the |
| 48 | * {@code @ExtendWith} annotations are discovered. For example, if a test class |
| 49 | * is annotated with {@code @ExtendWith(A.class)} and then with |
| 50 | * {@code @ExtendWith(B.class)}, extension {@code A} will be registered before |
| 51 | * extension {@code B}. |
| 52 | * |
| 53 | * <p>By default, if multiple extensions are registered on fields via |
| 54 | * {@code @ExtendWith}, they will be ordered using an algorithm that is |
| 55 | * deterministic but intentionally nonobvious. This ensures that subsequent runs |
| 56 | * of a test suite execute extensions in the same order, thereby allowing for |
| 57 | * repeatable builds. However, there are times when extensions need to be |
| 58 | * registered in an explicit order. To achieve that, you can annotate |
| 59 | * {@code @ExtendWith} fields with {@link org.junit.jupiter.api.Order @Order}. |
| 60 | * Any {@code @ExtendWith} field not annotated with {@code @Order} will be |
| 61 | * ordered using the {@link org.junit.jupiter.api.Order#DEFAULT default} order |
| 62 | * value. Note that {@code @RegisterExtension} fields can also be ordered with |
| 63 | * {@code @Order}, relative to {@code @ExtendWith} fields and other |
| 64 | * {@code @RegisterExtension} fields. |
| 65 | * |
| 66 | * <h2>Supported Extension APIs</h2> |
| 67 | * |
| 68 | * <ul> |
| 69 | * <li>{@link ExecutionCondition}</li> |
| 70 | * <li>{@link InvocationInterceptor}</li> |
| 71 | * <li>{@link BeforeAllCallback}</li> |
| 72 | * <li>{@link AfterAllCallback}</li> |
| 73 | * <li>{@link BeforeEachCallback}</li> |
| 74 | * <li>{@link AfterEachCallback}</li> |
| 75 | * <li>{@link BeforeTestExecutionCallback}</li> |
| 76 | * <li>{@link AfterTestExecutionCallback}</li> |
| 77 | * <li>{@link TestInstanceFactory}</li> |
| 78 | * <li>{@link TestInstancePostProcessor}</li> |
| 79 | * <li>{@link TestInstancePreConstructCallback}</li> |
| 80 | * <li>{@link TestInstancePreDestroyCallback}</li> |
| 81 | * <li>{@link ParameterResolver}</li> |
| 82 | * <li>{@link LifecycleMethodExecutionExceptionHandler}</li> |
| 83 | * <li>{@link TestExecutionExceptionHandler}</li> |
| 84 | * <li>{@link TestTemplateInvocationContextProvider}</li> |
| 85 | * <li>{@link TestWatcher}</li> |
| 86 | * </ul> |
| 87 | * |
| 88 | * @since 5.0 |
| 89 | * @see RegisterExtension |
| 90 | * @see Extension |
| 91 | */ |
| 92 | @Target({ ElementType.TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER }) |
| 93 | @Retention(RetentionPolicy.RUNTIME) |
| 94 | @Documented |
| 95 | @Inherited |
| 96 | @Repeatable(Extensions.class) |
| 97 | @API(status = STABLE, since = "5.0") |
| 98 | public @interface ExtendWith { |
| 99 | |
| 100 | /** |
| 101 | * An array of one or more {@link Extension} classes to register. |
| 102 | */ |
| 103 | Class<? extends Extension>[] value(); |
| 104 | |
| 105 | } |