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