|
10 | 10 |
|
11 | 11 | package org.junit.jupiter.api.extension; |
12 | 12 |
|
13 | | -import static org.apiguardian.api.API.Status.STABLE; |
| 13 | +import static org.apiguardian.api.API.Status.EXPERIMENTAL; |
14 | 14 |
|
15 | 15 | import org.apiguardian.api.API; |
16 | 16 |
|
17 | 17 | /** |
18 | 18 | * {@code LifecycleMethodExecutionExceptionHandler} defines the API for |
19 | 19 | * {@link Extension Extensions} that wish to handle exceptions thrown during |
20 | | - * execution of lifecycle methods (annotated with {@code @BeforeAll}, |
21 | | - * {@code @BeforeEach}, {@code @AfterEach} and {@code @AfterAll}. |
| 20 | + * the execution of {@code @BeforeAll}, {@code @BeforeEach}, {@code @AfterEach}, |
| 21 | + * and {@code @AfterAll} lifecycle methods. |
22 | 22 | * |
23 | 23 | * <p>Common use cases include swallowing an exception if it's anticipated, |
24 | | - * logging or rolling back a transaction in certain error scenarios. |
| 24 | + * logging errors, or rolling back a transaction in certain error scenarios. |
25 | 25 | * |
26 | | - * <p>This extension needs to be declared on a class level if class level methods |
27 | | - * ({@code @BeforeAll}, {@code @AfterAll}) are to be covered. If declared on Test |
28 | | - * level, only handlers for {@code @BeforeEach} and {@code @AfterEach} will execute |
| 26 | + * <p>Implementations of this extension API must be registered at the class level |
| 27 | + * if exceptions thrown from {@code @BeforeAll} or {@code @AfterAll} methods are |
| 28 | + * to be handled. When registered at the test level, only exceptions thrown from |
| 29 | + * {@code @BeforeEach} or {@code @AfterEach} methods will be handled. |
29 | 30 | * |
30 | 31 | * <h3>Constructor Requirements</h3> |
31 | 32 | * |
32 | 33 | * <p>Consult the documentation in {@link Extension} for details on constructor |
33 | 34 | * requirements. |
34 | 35 | * |
35 | | - * @see TestExecutionExceptionHandler |
| 36 | + * <h3 id="implementation-guidelines">Implementation Guidelines</h3> |
| 37 | + * |
| 38 | + * <p>An implementation of an exception handler method defined in this API must |
| 39 | + * perform one of the following. |
| 40 | + * |
| 41 | + * <ol> |
| 42 | + * <li>Rethrow the supplied {@code Throwable} <em>as is</em>, which is the default implementation.</li> |
| 43 | + * <li>Swallow the supplied {@code Throwable}, thereby preventing propagation.</li> |
| 44 | + * <li>Throw a new exception, potentially wrapping the supplied {@code Throwable}.</li> |
| 45 | + * </ol> |
| 46 | + * |
| 47 | + * <p>If the supplied {@code Throwable} is swallowed by a handler method, subsequent |
| 48 | + * handler methods for the same lifecycle will not be invoked; otherwise, the |
| 49 | + * corresponding handler method of the next registered |
| 50 | + * {@code LifecycleMethodExecutionExceptionHandler} (if there is one) will be |
| 51 | + * invoked with any {@link Throwable} thrown by the previous handler. |
36 | 52 | * |
| 53 | + * @see TestExecutionExceptionHandler |
37 | 54 | * @since 5.5 |
38 | 55 | */ |
39 | | -@API(status = STABLE, since = "5.5") |
| 56 | +@API(status = EXPERIMENTAL, since = "5.5") |
40 | 57 | public interface LifecycleMethodExecutionExceptionHandler extends Extension { |
41 | 58 |
|
42 | 59 | /** |
43 | | - * Handle the supplied {@link Throwable throwable}. |
44 | | - * |
45 | | - * <p>Implementors must perform one of the following. |
46 | | - * <ol> |
47 | | - * <li>Rethrow the supplied {@code throwable} <em>as is</em> which is the default implementation</li> |
48 | | - * <li>Swallow the supplied {@code throwable}, thereby preventing propagation.</li> |
49 | | - * <li>Throw a new exception, potentially wrapping the supplied {@code throwable}.</li> |
50 | | - * </ol> |
| 60 | + * Handle the supplied {@link Throwable} that was thrown during execution of |
| 61 | + * a {@code @BeforeAll} lifecycle method. |
51 | 62 | * |
52 | | - * <p>If the supplied {@code throwable} is swallowed, subsequent |
53 | | - * {@code LifecycleMethodExecutionExceptionHandler} will not be invoked; |
54 | | - * otherwise, the next registered {@code LifecycleMethodExecutionExceptionHandler} |
55 | | - * (if there is one) will be invoked with any {@link Throwable} thrown by |
56 | | - * this handler. |
| 63 | + * <p>Please refer to the class-level Javadoc for |
| 64 | + * <a href="#implementation-guidelines">Implementation Guidelines</a>. |
57 | 65 | * |
58 | 66 | * @param context the current extension context; never {@code null} |
59 | 67 | * @param throwable the {@code Throwable} to handle; never {@code null} |
60 | 68 | */ |
61 | 69 | default void handleBeforeAllMethodExecutionException(ExtensionContext context, Throwable throwable) |
62 | 70 | throws Throwable { |
| 71 | + |
63 | 72 | throw throwable; |
64 | 73 | } |
65 | 74 |
|
66 | 75 | /** |
67 | | - * Handle the supplied {@link Throwable throwable}. |
| 76 | + * Handle the supplied {@link Throwable} that was thrown during execution of |
| 77 | + * a {@code @BeforeEach} lifecycle method. |
68 | 78 | * |
69 | | - * <p>Implementors must perform one of the following. |
70 | | - * <ol> |
71 | | - * <li>Rethrow the supplied {@code throwable} <em>as is</em> which is the default implementation</li> |
72 | | - * <li>Swallow the supplied {@code throwable}, thereby preventing propagation.</li> |
73 | | - * <li>Throw a new exception, potentially wrapping the supplied {@code throwable}.</li> |
74 | | - * </ol> |
75 | | - * |
76 | | - * <p>If the supplied {@code throwable} is swallowed, subsequent |
77 | | - * {@code LifecycleMethodExecutionExceptionHandler} |
78 | | - * will not be invoked; otherwise, the next registered |
79 | | - * {@code LifecycleMethodExecutionExceptionHandler} (if there is one) |
80 | | - * will be invoked with any {@link Throwable} thrown by this handler. |
| 79 | + * <p>Please refer to the class-level Javadoc for |
| 80 | + * <a href="#implementation-guidelines">Implementation Guidelines</a>. |
81 | 81 | * |
82 | 82 | * @param context the current extension context; never {@code null} |
83 | 83 | * @param throwable the {@code Throwable} to handle; never {@code null} |
84 | 84 | */ |
85 | 85 | default void handleBeforeEachMethodExecutionException(ExtensionContext context, Throwable throwable) |
86 | 86 | throws Throwable { |
| 87 | + |
87 | 88 | throw throwable; |
88 | 89 | } |
89 | 90 |
|
90 | 91 | /** |
91 | | - * Handle the supplied {@link Throwable throwable}. |
| 92 | + * Handle the supplied {@link Throwable} that was thrown during execution of |
| 93 | + * a {@code @AfterEach} lifecycle method. |
92 | 94 | * |
93 | | - * <p>Implementors must perform one of the following. |
94 | | - * <ol> |
95 | | - * <li>Rethrow the supplied {@code throwable} <em>as is</em> which is the default implementation</li> |
96 | | - * <li>Swallow the supplied {@code throwable}, thereby preventing propagation.</li> |
97 | | - * <li>Throw a new exception, potentially wrapping the supplied {@code throwable}.</li> |
98 | | - * </ol> |
99 | | - * |
100 | | - * <p>If the supplied {@code throwable} is swallowed, subsequent |
101 | | - * {@code LifecycleMethodExecutionExceptionHandler} will not be invoked; |
102 | | - * otherwise, the next registered {@code LifecycleMethodExecutionExceptionHandler} |
103 | | - * (if there is one) will be invoked with any {@link Throwable} thrown by |
104 | | - * this handler. |
| 95 | + * <p>Please refer to the class-level Javadoc for |
| 96 | + * <a href="#implementation-guidelines">Implementation Guidelines</a>. |
105 | 97 | * |
106 | 98 | * @param context the current extension context; never {@code null} |
107 | 99 | * @param throwable the {@code Throwable} to handle; never {@code null} |
108 | 100 | */ |
109 | 101 | default void handleAfterEachMethodExecutionException(ExtensionContext context, Throwable throwable) |
110 | 102 | throws Throwable { |
| 103 | + |
111 | 104 | throw throwable; |
112 | 105 | } |
113 | 106 |
|
114 | 107 | /** |
115 | | - * Handle the supplied {@link Throwable throwable}. |
| 108 | + * Handle the supplied {@link Throwable} that was thrown during execution of |
| 109 | + * a {@code @AfterAll} lifecycle method. |
116 | 110 | * |
117 | | - * <p>Implementors must perform one of the following. |
118 | | - * <ol> |
119 | | - * <li>Rethrow the supplied {@code throwable} <em>as is</em> which is the default implementation</li> |
120 | | - * <li>Swallow the supplied {@code throwable}, thereby preventing propagation.</li> |
121 | | - * <li>Throw a new exception, potentially wrapping the supplied {@code throwable}.</li> |
122 | | - * </ol> |
123 | | - * |
124 | | - * <p>If the supplied {@code throwable} is swallowed, subsequent |
125 | | - * {@code LifecycleMethodExecutionExceptionHandler} will not be invoked; otherwise, |
126 | | - * the next registered {@code LifecycleMethodExecutionExceptionHandler} (if there |
127 | | - * is one) will be invoked with any {@link Throwable} thrown by this handler. |
| 111 | + * <p>Please refer to the class-level Javadoc for |
| 112 | + * <a href="#implementation-guidelines">Implementation Guidelines</a>. |
128 | 113 | * |
129 | 114 | * @param context the current extension context; never {@code null} |
130 | 115 | * @param throwable the {@code Throwable} to handle; never {@code null} |
131 | 116 | */ |
132 | 117 | default void handleAfterAllMethodExecutionException(ExtensionContext context, Throwable throwable) |
133 | 118 | throws Throwable { |
| 119 | + |
134 | 120 | throw throwable; |
135 | 121 | } |
| 122 | + |
136 | 123 | } |
0 commit comments