junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/RegisterExtension.java
raw symbols SCIP| 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.Retention; |
| 18 | import java.lang.annotation.RetentionPolicy; |
| 19 | import java.lang.annotation.Target; |
| 20 | |
| 21 | import org.apiguardian.api.API; |
| 22 | |
| 23 | /** |
| 24 | * {@code @RegisterExtension} is used to register an {@link Extension} via a |
| 25 | * field in a test class. |
| 26 | * |
| 27 | * <p>In contrast to {@link ExtendWith @ExtendWith} which is used to register |
| 28 | * extensions <em>declaratively</em>, {@code @RegisterExtension} can be used to |
| 29 | * register an extension <em>programmatically</em> — for example, in order |
| 30 | * to pass arguments to the extension's constructor, {@code static} factory |
| 31 | * method, or builder API. |
| 32 | * |
| 33 | * <p>{@code @RegisterExtension} fields must not be {@code null} (when evaluated) |
| 34 | * but may be either {@code static} or non-static. |
| 35 | * |
| 36 | * <h2>Static Fields</h2> |
| 37 | * |
| 38 | * <p>If a {@code @RegisterExtension} field is {@code static}, the extension |
| 39 | * will be registered after extensions that are registered at the class level |
| 40 | * via {@code @ExtendWith}. Such <em>static</em> extensions are not limited in |
| 41 | * which extension APIs they can implement. Extensions registered via static |
| 42 | * fields may therefore implement class-level and instance-level extension APIs |
| 43 | * such as {@link BeforeAllCallback}, {@link AfterAllCallback}, |
| 44 | * {@link TestInstanceFactory}, {@link TestInstancePostProcessor} and |
| 45 | * {@link TestInstancePreDestroyCallback} as well as method-level extension APIs |
| 46 | * such as {@link BeforeEachCallback}, etc. |
| 47 | * |
| 48 | * <h2>Instance Fields</h2> |
| 49 | * |
| 50 | * <p>If a {@code @RegisterExtension} field is non-static (i.e., an instance |
| 51 | * field), the extension will be registered after the test class has been |
| 52 | * instantiated and after all {@link TestInstancePostProcessor |
| 53 | * TestInstancePostProcessors} have been given a chance to post-process the |
| 54 | * test instance (potentially injecting the instance of the extension to be |
| 55 | * used into the annotated field). Thus, if such an <em>instance</em> extension |
| 56 | * implements class-level or instance-level extension APIs such as |
| 57 | * {@link BeforeAllCallback}, {@link AfterAllCallback}, |
| 58 | * {@link TestInstanceFactory}, or {@link TestInstancePostProcessor} those APIs |
| 59 | * will not be honored. By default, an instance extension will be registered |
| 60 | * <em>after</em> extensions that are registered at the method level via |
| 61 | * {@code @ExtendWith}; however, if the test class is configured with |
| 62 | * {@link org.junit.jupiter.api.TestInstance.Lifecycle @TestInstance(Lifecycle.PER_CLASS)} |
| 63 | * semantics, an instance extension will be registered <em>before</em> extensions |
| 64 | * that are registered at the method level via {@code @ExtendWith}. |
| 65 | * |
| 66 | * <h2>Inheritance</h2> |
| 67 | * |
| 68 | * <p>{@code @RegisterExtension} fields are inherited from superclasses as long |
| 69 | * as they are not <em>hidden</em> or <em>overridden</em>. Furthermore, |
| 70 | * {@code @RegisterExtension} fields from superclasses will be registered before |
| 71 | * {@code @RegisterExtension} fields in subclasses. |
| 72 | * |
| 73 | * <h2>Registration Order</h2> |
| 74 | * |
| 75 | * <p>By default, if multiple extensions are registered via |
| 76 | * {@code @RegisterExtension}, they will be ordered using an algorithm that is |
| 77 | * deterministic but intentionally nonobvious. This ensures that subsequent runs |
| 78 | * of a test suite execute extensions in the same order, thereby allowing for |
| 79 | * repeatable builds. However, there are times when extensions need to be |
| 80 | * registered in an explicit order. To achieve that, you can annotate |
| 81 | * {@code @RegisterExtension} fields with {@link org.junit.jupiter.api.Order @Order}. |
| 82 | * Any {@code @RegisterExtension} field not annotated with {@code @Order} will be |
| 83 | * ordered using the {@link org.junit.jupiter.api.Order#DEFAULT default} order |
| 84 | * value. Note that {@code @ExtendWith} fields can also be ordered with |
| 85 | * {@code @Order}, relative to {@code @RegisterExtension} fields and other |
| 86 | * {@code @ExtendWith} fields. |
| 87 | * |
| 88 | * <h2>Example Usage</h2> |
| 89 | * |
| 90 | * <p>In the following example, the {@code docs} field in the test class is |
| 91 | * initialized programmatically by supplying a custom {@code lookUpDocsDir()} |
| 92 | * method to a {@code static} factory method in the {@code DocumentationExtension}. |
| 93 | * The configured {@code DocumentationExtension} will be automatically registered |
| 94 | * as an extension. In addition, test methods can access the instance of the |
| 95 | * extension via the {@code docs} field if necessary. |
| 96 | * |
| 97 | * <pre style="code"> |
| 98 | * class DocumentationTests { |
| 99 | * |
| 100 | * static Path lookUpDocsDir() { |
| 101 | * // return path to docs dir |
| 102 | * } |
| 103 | * |
| 104 | * {@literal @}RegisterExtension |
| 105 | * DocumentationExtension docs = |
| 106 | * DocumentationExtension.forPath(lookUpDocsDir()); |
| 107 | * |
| 108 | * {@literal @}Test |
| 109 | * void generateDocumentation() { |
| 110 | * // use docs ... |
| 111 | * } |
| 112 | * }</pre> |
| 113 | * |
| 114 | * <h2>Supported Extension APIs</h2> |
| 115 | * |
| 116 | * <ul> |
| 117 | * <li>{@link ExecutionCondition}</li> |
| 118 | * <li>{@link InvocationInterceptor}</li> |
| 119 | * <li>{@link BeforeAllCallback}</li> |
| 120 | * <li>{@link AfterAllCallback}</li> |
| 121 | * <li>{@link BeforeEachCallback}</li> |
| 122 | * <li>{@link AfterEachCallback}</li> |
| 123 | * <li>{@link BeforeTestExecutionCallback}</li> |
| 124 | * <li>{@link AfterTestExecutionCallback}</li> |
| 125 | * <li>{@link TestInstanceFactory}</li> |
| 126 | * <li>{@link TestInstancePostProcessor}</li> |
| 127 | * <li>{@link TestInstancePreConstructCallback}</li> |
| 128 | * <li>{@link TestInstancePreDestroyCallback}</li> |
| 129 | * <li>{@link ParameterResolver}</li> |
| 130 | * <li>{@link LifecycleMethodExecutionExceptionHandler}</li> |
| 131 | * <li>{@link TestExecutionExceptionHandler}</li> |
| 132 | * <li>{@link TestTemplateInvocationContextProvider}</li> |
| 133 | * <li>{@link TestWatcher}</li> |
| 134 | * </ul> |
| 135 | * |
| 136 | * @since 5.1 |
| 137 | * @see ExtendWith @ExtendWith |
| 138 | * @see Extension |
| 139 | * @see org.junit.jupiter.api.Order @Order |
| 140 | */ |
| 141 | @Target(ElementType.FIELD) |
| 142 | @Retention(RetentionPolicy.RUNTIME) |
| 143 | @Documented |
| 144 | @API(status = STABLE, since = "5.1") |
| 145 | public @interface RegisterExtension { |
| 146 | } |