public interface TestExecutionListener
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.
Spring provides the following out-of-the-box implementations (all of
which implement Ordered
):
@TestExecutionListeners
,
TestContextManager
,
AbstractTestExecutionListener
Modifier and Type | Method and Description |
---|---|
default 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 by injecting dependencies. |
default void beforeTestClass(TestContext testContext) throws Exception
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.
testContext
- the test context for the test; never null
Exception
- allows any exception to propagatedefault void prepareTestInstance(TestContext testContext) throws Exception
test instance
of the supplied
test context
, for example by injecting dependencies.
This method should be called immediately after instantiation of the test instance but prior to any framework-specific lifecycle callbacks.
The default implementation is empty. Can be overridden by concrete classes as necessary.
testContext
- the test context for the test; never null
Exception
- allows any exception to propagatedefault void beforeTestMethod(TestContext testContext) throws Exception
This method must be called immediately prior to
framework-specific before lifecycle callbacks. For historical
reasons, this method is named beforeTestMethod
. Since the
introduction of beforeTestExecution(org.springframework.test.context.TestContext)
, a more suitable name for
this method might be something like beforeTestSetUp
or
beforeEach
; 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.
testContext
- the test context in which the test method will be
executed; never null
Exception
- allows any exception to propagateafterTestMethod(org.springframework.test.context.TestContext)
,
beforeTestExecution(org.springframework.test.context.TestContext)
,
afterTestExecution(org.springframework.test.context.TestContext)
default void beforeTestExecution(TestContext testContext) throws Exception
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.
testContext
- the test context in which the test method will be
executed; never null
Exception
- allows any exception to propagatebeforeTestMethod(org.springframework.test.context.TestContext)
,
afterTestMethod(org.springframework.test.context.TestContext)
,
afterTestExecution(org.springframework.test.context.TestContext)
default void afterTestExecution(TestContext testContext) throws Exception
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.
testContext
- the test context in which the test method will be
executed; never null
Exception
- allows any exception to propagatebeforeTestMethod(org.springframework.test.context.TestContext)
,
afterTestMethod(org.springframework.test.context.TestContext)
,
beforeTestExecution(org.springframework.test.context.TestContext)
default void afterTestMethod(TestContext testContext) throws Exception
This method must be called immediately after
framework-specific after lifecycle callbacks. For historical
reasons, this method is named afterTestMethod
. Since the
introduction of afterTestExecution(org.springframework.test.context.TestContext)
, a more suitable name for
this method might be something like afterTestTearDown
or
afterEach
; 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.
testContext
- the test context in which the test method was
executed; never null
Exception
- allows any exception to propagatebeforeTestMethod(org.springframework.test.context.TestContext)
,
beforeTestExecution(org.springframework.test.context.TestContext)
,
afterTestExecution(org.springframework.test.context.TestContext)
default void afterTestClass(TestContext testContext) throws Exception
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.
testContext
- the test context for the test; never null
Exception
- allows any exception to propagate