Skip to content

Commit 39ea6d6

Browse files
author
Mateusz Pietryga
committed
LifecycleMethodExecutionExceptionHandler API description
1 parent cd4bbfd commit 39ea6d6

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/LifecycleMethodExecutionExceptionHandler.java

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,121 @@
11
package org.junit.jupiter.api.extension;
22

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")
329
public interface LifecycleMethodExecutionExceptionHandler extends Extension {
430

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+
*/
550
default void handleBeforeAllMethodExecutionException(ExtensionContext context, Throwable throwable) throws Throwable {
651
throw throwable;
752
}
853

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+
*/
974
default void handleBeforeEachMethodExecutionException(ExtensionContext context, Throwable throwable) throws Throwable {
1075
throw throwable;
1176
}
1277

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+
*/
1397
default void handleAfterEachMethodExecutionException(ExtensionContext context, Throwable throwable) throws Throwable {
1498
throw throwable;
1599
}
16100

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+
*/
17119
default void handleAfterAllMethodExecutionException(ExtensionContext context, Throwable throwable) throws Throwable {
18120
throw throwable;
19121
}

0 commit comments

Comments
 (0)