junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/TestExecutionExceptionHandler.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 TestExecutionExceptionHandler} defines the API for {@link Extension |
| 19 | * Extensions} that wish to handle exceptions thrown during test execution. |
| 20 | * |
| 21 | * <p>In this context, <em>test execution</em> refers to the physical |
| 22 | * invocation of a {@code @Test} method and not to any test-level extensions |
| 23 | * or callbacks. |
| 24 | * |
| 25 | * <p>Common use cases include swallowing an exception if it's anticipated |
| 26 | * or rolling back a transaction in certain error scenarios. |
| 27 | * |
| 28 | * <h2>Constructor Requirements</h2> |
| 29 | * |
| 30 | * <p>Consult the documentation in {@link Extension} for details on |
| 31 | * constructor requirements. |
| 32 | * |
| 33 | * @since 5.0 |
| 34 | * @see LifecycleMethodExecutionExceptionHandler |
| 35 | */ |
| 36 | @FunctionalInterface |
| 37 | @API(status = STABLE, since = "5.0") |
| 38 | public interface TestExecutionExceptionHandler extends Extension { |
| 39 | |
| 40 | /** |
| 41 | * Handle the supplied {@link Throwable throwable}. |
| 42 | * |
| 43 | * <p>Implementors must perform one of the following. |
| 44 | * <ol> |
| 45 | * <li>Swallow the supplied {@code throwable}, thereby preventing propagation.</li> |
| 46 | * <li>Rethrow the supplied {@code throwable} <em>as is</em>.</li> |
| 47 | * <li>Throw a new exception, potentially wrapping the supplied {@code throwable}.</li> |
| 48 | * </ol> |
| 49 | * |
| 50 | * <p>If the supplied {@code throwable} is swallowed, subsequent |
| 51 | * {@code TestExecutionExceptionHandlers} will not be invoked; otherwise, |
| 52 | * the next registered {@code TestExecutionExceptionHandler} (if there is |
| 53 | * one) will be invoked with any {@link Throwable} thrown by this handler. |
| 54 | * |
| 55 | * <p>Note that the {@link ExtensionContext#getExecutionException() execution |
| 56 | * exception} in the supplied {@code ExtensionContext} will <em>not</em> |
| 57 | * contain the {@code Throwable} thrown during invocation of the corresponding |
| 58 | * {@code @Test} method. |
| 59 | * |
| 60 | * @param context the current extension context; never {@code null} |
| 61 | * @param throwable the {@code Throwable} to handle; never {@code null} |
| 62 | */ |
| 63 | void handleTestExecutionException(ExtensionContext context, Throwable throwable) throws Throwable; |
| 64 | |
| 65 | } |