Interface TestExecutionListener
- All Known Subinterfaces:
AotTestExecutionListener
- All Known Implementing Classes:
AbstractDirtiesContextTestExecutionListener
,AbstractTestExecutionListener
,ApplicationEventsTestExecutionListener
,DependencyInjectionTestExecutionListener
,DirtiesContextBeforeModesTestExecutionListener
,DirtiesContextTestExecutionListener
,EventPublishingTestExecutionListener
,ServletTestExecutionListener
,SqlScriptsTestExecutionListener
,TransactionalTestExecutionListener
TestExecutionListener
defines a listener API for reacting to
test execution events published by the TestContextManager
with which
the listener is registered.
Note that not all testing frameworks support all lifecycle callbacks defined
in this API. For example, beforeTestExecution(org.springframework.test.context.TestContext)
and
afterTestExecution(org.springframework.test.context.TestContext)
are not supported in conjunction with JUnit 4 when
using the SpringMethodRule
.
This interface provides empty default
implementations for all methods.
Concrete implementations can therefore choose to override only those methods
suitable for the task at hand.
Concrete implementations must provide a public
no-args constructor,
so that listeners can be instantiated transparently by tools and configuration
mechanisms.
Implementations may optionally declare the position in which they should
be ordered among the chain of default listeners via the
Ordered
interface or
@Order
annotation. See
TestContextBootstrapper.getTestExecutionListeners()
for details.
Registering TestExecutionListener Implementations
A TestExecutionListener
can be registered explicitly for a test class,
its subclasses, and its nested classes by using the
@TestExecutionListeners
annotation. Explicit
registration is suitable for custom listeners that are used in limited testing
scenarios. However, it can become cumbersome if a custom listener needs to be
used across an entire test suite. This issue is addressed through support for
automatic discovery of default TestExecutionListener
implementations through the
SpringFactoriesLoader
mechanism. Specifically, default TestExecutionListener
implementations
can be registered under the org.springframework.test.context.TestExecutionListener
key in a META-INF/spring.factories
properties file.
Spring provides the following implementations. Each of these implements
Ordered
and is registered automatically by default.
ServletTestExecutionListener
DirtiesContextBeforeModesTestExecutionListener
ApplicationEventsTestExecutionListener
DependencyInjectionTestExecutionListener
MicrometerObservationRegistryTestExecutionListener
DirtiesContextTestExecutionListener
TransactionalTestExecutionListener
SqlScriptsTestExecutionListener
EventPublishingTestExecutionListener
- Since:
- 2.5
- Author:
- Sam Brannen, Juergen Hoeller
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptiondefault void
afterTestClass
(TestContext testContext) Post-processes a test class after execution of all tests within the class.default void
afterTestExecution
(TestContext testContext) Post-processes a test immediately after execution of the test method in the supplied test context — for example, for timing or logging purposes.default void
afterTestMethod
(TestContext testContext) Post-processes a test after execution of after lifecycle callbacks of the underlying test framework — for example, by tearing down test fixtures.default void
beforeTestClass
(TestContext testContext) Pre-processes a test class before execution of all tests within the class.default void
beforeTestExecution
(TestContext testContext) Pre-processes a test immediately before execution of the test method in the supplied test context — for example, for timing or logging purposes.default void
beforeTestMethod
(TestContext testContext) Pre-processes a test before execution of before lifecycle callbacks of the underlying test framework — for example, by setting up test fixtures.default void
prepareTestInstance
(TestContext testContext) Prepares the test instance of the supplied test context — for example, to inject dependencies.
-
Method Details
-
beforeTestClass
Pre-processes a test class before execution of all tests within the class.This method should be called immediately before framework-specific before class lifecycle callbacks.
The default implementation is empty. Can be overridden by concrete classes as necessary.
- Parameters:
testContext
- the test context for the test; nevernull
- Throws:
Exception
- allows any exception to propagate- Since:
- 3.0
-
prepareTestInstance
Prepares the test instance of the supplied test context — for example, to inject dependencies.This method should be called immediately after instantiation of the test class or as soon after instantiation as possible (as is the case with the
SpringMethodRule
). In any case, this method must be called prior to any framework-specific lifecycle callbacks.The default implementation is empty. Can be overridden by concrete classes as necessary.
- Parameters:
testContext
- the test context for the test; nevernull
- Throws:
Exception
- allows any exception to propagate
-
beforeTestMethod
Pre-processes a test before execution of before lifecycle callbacks of the underlying test framework — for example, by setting up test fixtures.This method must be called immediately prior to framework-specific before lifecycle callbacks. For historical reasons, this method is named
beforeTestMethod
. Since the introduction ofbeforeTestExecution(org.springframework.test.context.TestContext)
, a more suitable name for this method might be something likebeforeTestSetUp
orbeforeEach
; however, it is unfortunately impossible to rename this method due to backward compatibility concerns.The default implementation is empty. Can be overridden by concrete classes as necessary.
- Parameters:
testContext
- the test context in which the test method will be executed; nevernull
- Throws:
Exception
- allows any exception to propagate- See Also:
-
beforeTestExecution
Pre-processes a test immediately before execution of the test method in the supplied test context — for example, for timing or logging purposes.This method must be called after framework-specific before lifecycle callbacks.
The default implementation is empty. Can be overridden by concrete classes as necessary.
- Parameters:
testContext
- the test context in which the test method will be executed; nevernull
- Throws:
Exception
- allows any exception to propagate- Since:
- 5.0
- See Also:
-
afterTestExecution
Post-processes a test immediately after execution of the test method in the supplied test context — for example, for timing or logging purposes.This method must be called before framework-specific after lifecycle callbacks.
The default implementation is empty. Can be overridden by concrete classes as necessary.
- Parameters:
testContext
- the test context in which the test method will be executed; nevernull
- Throws:
Exception
- allows any exception to propagate- Since:
- 5.0
- See Also:
-
afterTestMethod
Post-processes a test after execution of after lifecycle callbacks of the underlying test framework — for example, by tearing down test fixtures.This method must be called immediately after framework-specific after lifecycle callbacks. For historical reasons, this method is named
afterTestMethod
. Since the introduction ofafterTestExecution(org.springframework.test.context.TestContext)
, a more suitable name for this method might be something likeafterTestTearDown
orafterEach
; however, it is unfortunately impossible to rename this method due to backward compatibility concerns.The default implementation is empty. Can be overridden by concrete classes as necessary.
- Parameters:
testContext
- the test context in which the test method was executed; nevernull
- Throws:
Exception
- allows any exception to propagate- See Also:
-
afterTestClass
Post-processes a test class after execution of all tests within the class.This method should be called immediately after framework-specific after class lifecycle callbacks.
The default implementation is empty. Can be overridden by concrete classes as necessary.
- Parameters:
testContext
- the test context for the test; nevernull
- Throws:
Exception
- allows any exception to propagate- Since:
- 3.0
-