Debian Codegraph @

junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/LifecycleMethodExecutionExceptionHandler.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 org.apiguardian.api.API;
16
17 /**
18 * {@code LifecycleMethodExecutionExceptionHandler} defines the API for
19 * {@link Extension Extensions} that wish to handle exceptions thrown during
20 * the execution of {@code @BeforeAll}, {@code @BeforeEach}, {@code @AfterEach},
21 * and {@code @AfterAll} lifecycle methods.
22 *
23 * <p>Common use cases include swallowing an exception if it's anticipated,
24 * logging errors, or rolling back a transaction in certain error scenarios.
25 *
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.
30 *
31 * <h2>Constructor Requirements</h2>
32 *
33 * <p>Consult the documentation in {@link Extension} for details on constructor
34 * requirements.
35 *
36 * <h2 id="implementation-guidelines">Implementation Guidelines</h2>
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.
52 *
53 * @since 5.5
54 * @see TestExecutionExceptionHandler
55 */
56 @API(status = STABLE, since = "5.10")
57 public interface LifecycleMethodExecutionExceptionHandler extends Extension {
58
59 /**
60 * Handle the supplied {@link Throwable} that was thrown during execution of
61 * a {@code @BeforeAll} lifecycle method.
62 *
63 * <p>Please refer to the class-level Javadoc for
64 * <a href="#implementation-guidelines">Implementation Guidelines</a>.
65 *
66 * @param context the current extension context; never {@code null}
67 * @param throwable the {@code Throwable} to handle; never {@code null}
68 */
69 default void handleBeforeAllMethodExecutionException(ExtensionContext context, Throwable throwable)
70 throws Throwable {
71
72 throw throwable;
73 }
74
75 /**
76 * Handle the supplied {@link Throwable} that was thrown during execution of
77 * a {@code @BeforeEach} lifecycle method.
78 *
79 * <p>Please refer to the class-level Javadoc for
80 * <a href="#implementation-guidelines">Implementation Guidelines</a>.
81 *
82 * @param context the current extension context; never {@code null}
83 * @param throwable the {@code Throwable} to handle; never {@code null}
84 */
85 default void handleBeforeEachMethodExecutionException(ExtensionContext context, Throwable throwable)
86 throws Throwable {
87
88 throw throwable;
89 }
90
91 /**
92 * Handle the supplied {@link Throwable} that was thrown during execution of
93 * a {@code @AfterEach} lifecycle method.
94 *
95 * <p>Please refer to the class-level Javadoc for
96 * <a href="#implementation-guidelines">Implementation Guidelines</a>.
97 *
98 * @param context the current extension context; never {@code null}
99 * @param throwable the {@code Throwable} to handle; never {@code null}
100 */
101 default void handleAfterEachMethodExecutionException(ExtensionContext context, Throwable throwable)
102 throws Throwable {
103
104 throw throwable;
105 }
106
107 /**
108 * Handle the supplied {@link Throwable} that was thrown during execution of
109 * a {@code @AfterAll} lifecycle method.
110 *
111 * <p>Please refer to the class-level Javadoc for
112 * <a href="#implementation-guidelines">Implementation Guidelines</a>.
113 *
114 * @param context the current extension context; never {@code null}
115 * @param throwable the {@code Throwable} to handle; never {@code null}
116 */
117 default void handleAfterAllMethodExecutionException(ExtensionContext context, Throwable throwable)
118 throws Throwable {
119
120 throw throwable;
121 }
122
123 }