| 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.util.Optional; |
| 16 | |
| 17 | import org.apiguardian.api.API; |
| 18 | |
| 19 | /** |
| 20 | * {@code TestWatcher} defines the API for {@link Extension Extensions} that |
| 21 | * wish to process test results. |
| 22 | * |
| 23 | * <p>The methods in this API are called after a test has been skipped or |
| 24 | * executed. Any {@link ExtensionContext.Store.CloseableResource CloseableResource} |
| 25 | * objects stored in the {@link ExtensionContext.Store Store} of the supplied |
| 26 | * {@link ExtensionContext} will have already been <strong>closed</strong> before |
| 27 | * methods in this API are invoked. |
| 28 | * |
| 29 | * <p>Please note that this API is currently only used to report the results of |
| 30 | * {@link org.junit.jupiter.api.Test @Test} methods and |
| 31 | * {@link org.junit.jupiter.api.TestTemplate @TestTemplate} methods (e.g., |
| 32 | * {@code @RepeatedTest} and {@code @ParameterizedTest}). Moreover, if there is a |
| 33 | * failure at the class level — for example, an exception thrown by a |
| 34 | * {@code @BeforeAll} method — no test results will be reported. Similarly, |
| 35 | * if the test class is disabled via an {@link ExecutionCondition} — for |
| 36 | * example, {@code @Disabled} — no test results will be reported. |
| 37 | * |
| 38 | * <p>Extensions implementing this interface can be registered at the class level, |
| 39 | * instance level, or method level. When registered at the class level, a |
| 40 | * {@code TestWatcher} will be invoked for any contained test method including |
| 41 | * those in {@link org.junit.jupiter.api.Nested @Nested} classes. When registered |
| 42 | * at the method level, a {@code TestWatcher} will only be invoked for the test |
| 43 | * method for which it was registered. |
| 44 | * |
| 45 | * <p><strong>WARNING</strong>: If a {@code TestWatcher} is registered via a |
| 46 | * non-static (instance) field — for example, using |
| 47 | * {@link RegisterExtension @RegisterExtension} — and the test class is |
| 48 | * configured with |
| 49 | * {@link org.junit.jupiter.api.TestInstance @TestInstance(Lifecycle.PER_METHOD)} |
| 50 | * semantics (which is the default lifecycle mode), the {@code TestWatcher} will |
| 51 | * <strong>not</strong> be invoked with events for {@code @TestTemplate} methods |
| 52 | * (such as {@code @RepeatedTest} and {@code @ParameterizedTest}). To ensure that |
| 53 | * a {@code TestWatcher} is invoked for all test methods in a given class, it is |
| 54 | * therefore recommended that the {@code TestWatcher} be registered at the class |
| 55 | * level with {@link ExtendWith @ExtendWith} or via a {@code static} field with |
| 56 | * {@code @RegisterExtension} or {@code @ExtendWith}. |
| 57 | * |
| 58 | * <h2>Exception Handling</h2> |
| 59 | * |
| 60 | * <p>In contrast to other {@link Extension} APIs, a {@code TestWatcher} is not |
| 61 | * permitted to adversely influence the execution of tests. Consequently, any |
| 62 | * exception thrown by a method in the {@code TestWatcher} API will be logged at |
| 63 | * {@code WARNING} level and will not be allowed to propagate or fail test |
| 64 | * execution. |
| 65 | * |
| 66 | * @since 5.4 |
| 67 | */ |
| 68 | @API(status = STABLE, since = "5.7") |
| 69 | public interface TestWatcher extends Extension { |
| 70 | |
| 71 | /** |
| 72 | * Invoked after a disabled test has been skipped. |
| 73 | * |
| 74 | * <p>The default implementation does nothing. Concrete implementations can |
| 75 | * override this method as appropriate. |
| 76 | * |
| 77 | * @param context the current extension context; never {@code null} |
| 78 | * @param reason the reason the test is disabled; never {@code null} but |
| 79 | * potentially <em>empty</em> |
| 80 | */ |
| 81 | default void testDisabled(ExtensionContext context, Optional<String> reason) { |
| 82 | /* no-op */ |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Invoked after a test has completed successfully. |
| 87 | * |
| 88 | * <p>The default implementation does nothing. Concrete implementations can |
| 89 | * override this method as appropriate. |
| 90 | * |
| 91 | * @param context the current extension context; never {@code null} |
| 92 | */ |
| 93 | default void testSuccessful(ExtensionContext context) { |
| 94 | /* no-op */ |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Invoked after a test has been aborted. |
| 99 | * |
| 100 | * <p>The default implementation does nothing. Concrete implementations can |
| 101 | * override this method as appropriate. |
| 102 | * |
| 103 | * @param context the current extension context; never {@code null} |
| 104 | * @param cause the throwable responsible for the test being aborted; may be {@code null} |
| 105 | */ |
| 106 | default void testAborted(ExtensionContext context, Throwable cause) { |
| 107 | /* no-op */ |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Invoked after a test has failed. |
| 112 | * |
| 113 | * <p>The default implementation does nothing. Concrete implementations can |
| 114 | * override this method as appropriate. |
| 115 | * |
| 116 | * @param context the current extension context; never {@code null} |
| 117 | * @param cause the throwable that caused test failure; may be {@code null} |
| 118 | */ |
| 119 | default void testFailed(ExtensionContext context, Throwable cause) { |
| 120 | /* no-op */ |
| 121 | } |
| 122 | |
| 123 | } |