TestWatcher defines the API for Extensions that
wish to process test results.
The methods in this API are called after a test has been skipped or
executed. Any CloseableResource
objects stored in the Store of the supplied
ExtensionContext will have already been closed before
methods in this API are invoked.
Please note that this API is currently only used to report the results of
@Test methods and
@TestTemplate methods (e.g.,
@RepeatedTest and @ParameterizedTest). Moreover, if there is a
failure at the class level — for example, an exception thrown by a
@BeforeAll method — no test results will be reported. Similarly,
if the test class is disabled via an ExecutionCondition — for
example, @Disabled — no test results will be reported.
Extensions implementing this interface can be registered at the class level,
instance level, or method level. When registered at the class level, a
TestWatcher will be invoked for any contained test method including
those in @Nested classes. When registered
at the method level, a TestWatcher will only be invoked for the test
method for which it was registered.
Warning
If a TestWatcher is registered via a
non-static (instance) field — for example, using
@RegisterExtension — and the test class is
configured with
@TestInstance(Lifecycle.
semantics (which is the default lifecycle mode), the TestWatcher will
not be invoked with events for @TestTemplate methods
(such as @RepeatedTest and @ParameterizedTest). To ensure that
a TestWatcher is invoked for all test methods in a given class, it is
therefore recommended that the TestWatcher be registered at the class
level with @ExtendWith or via a static field with
@RegisterExtension or @ExtendWith.
In contrast to other Extension APIs, a TestWatcher is not
permitted to adversely influence the execution of tests. Consequently, any
exception thrown by a method in the TestWatcher API will be logged at
WARNING level and will not be allowed to propagate or fail test
execution.
| Modifier and Type | Method and Description |
|---|---|
| public default void | testAborted(ExtensionContext
the current extension context; never context, @Nullable Throwable nullthe throwable responsible for the test being aborted; may be cause)nullInvoked after a test has been aborted. |
| public default void | testDisabled(ExtensionContext
the current extension context; never context, Optional<String> nullthe reason the test is disabled; never reason)null but
potentially emptyInvoked after a disabled test has been skipped. |
| public default void | testFailed(ExtensionContext
the current extension context; never context, @Nullable Throwable nullthe throwable that caused test failure; may be cause)nullInvoked after a test has failed. |
| public default void | testSuccessful(ExtensionContext
the current extension context; never context)nullInvoked after a test has completed successfully. |
| testAborted | back to summary |
|---|---|
| public default void testAborted(ExtensionContext context, @Nullable Throwable cause) Invoked after a test has been aborted. The default implementation does nothing. Concrete implementations can override this method as appropriate.
| |
| testDisabled | back to summary |
|---|---|
| public default void testDisabled(ExtensionContext context, Optional<String> reason) Invoked after a disabled test has been skipped. The default implementation does nothing. Concrete implementations can override this method as appropriate.
| |
| testFailed | back to summary |
|---|---|
| public default void testFailed(ExtensionContext context, @Nullable Throwable cause) Invoked after a test has failed. The default implementation does nothing. Concrete implementations can override this method as appropriate.
| |
| testSuccessful | back to summary |
|---|---|
| public default void testSuccessful(ExtensionContext context) Invoked after a test has completed successfully. The default implementation does nothing. Concrete implementations can override this method as appropriate.
| |