Interface TestExecutionListener
- All Known Implementing Classes:
AbstractDirtiesContextTestExecutionListener
,AbstractTestExecutionListener
,DependencyInjectionTestExecutionListener
,DirtiesContextBeforeModesTestExecutionListener
,DirtiesContextTestExecutionListener
,EventPublishingTestExecutionListener
,ServletTestExecutionListener
,SqlScriptsTestExecutionListener
,TransactionalTestExecutionListener
public interface TestExecutionListener
TestExecutionListener
defines a listener API for reacting to test execution events published by theTestContextManager
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)
andafterTestExecution(org.springframework.test.context.TestContext)
are not supported in conjunction with JUnit 4 when using theSpringMethodRule
.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. SeeTestContextBootstrapper.getTestExecutionListeners()
for details.Spring provides the following out-of-the-box implementations (all of which implement
Ordered
):- Since:
- 2.5
- Author:
- Sam Brannen, Juergen Hoeller
- See Also:
@TestExecutionListeners
,TestContextManager
,AbstractTestExecutionListener
Method Summary
All Methods Instance Methods Default Methods Modifier and Type Method 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 thetest method
in the suppliedtest 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 thetest method
in the suppliedtest 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 thetest instance
of the suppliedtest context
, for example by injecting dependencies.
Method Detail
beforeTestClass
default void beforeTestClass(TestContext testContext) throws Exception
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
default void prepareTestInstance(TestContext testContext) throws Exception
Prepares thetest instance
of the suppliedtest 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.
- Parameters:
testContext
- the test context for the test; nevernull
- Throws:
Exception
- allows any exception to propagate
beforeTestMethod
default void beforeTestMethod(TestContext testContext) throws Exception
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:
afterTestMethod(org.springframework.test.context.TestContext)
,beforeTestExecution(org.springframework.test.context.TestContext)
,afterTestExecution(org.springframework.test.context.TestContext)
beforeTestExecution
default void beforeTestExecution(TestContext testContext) throws Exception
Pre-processes a test immediately before execution of thetest method
in the suppliedtest 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:
beforeTestMethod(org.springframework.test.context.TestContext)
,afterTestMethod(org.springframework.test.context.TestContext)
,afterTestExecution(org.springframework.test.context.TestContext)
afterTestExecution
default void afterTestExecution(TestContext testContext) throws Exception
Post-processes a test immediately after execution of thetest method
in the suppliedtest 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:
beforeTestMethod(org.springframework.test.context.TestContext)
,afterTestMethod(org.springframework.test.context.TestContext)
,beforeTestExecution(org.springframework.test.context.TestContext)
afterTestMethod
default void afterTestMethod(TestContext testContext) throws Exception
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:
beforeTestMethod(org.springframework.test.context.TestContext)
,beforeTestExecution(org.springframework.test.context.TestContext)
,afterTestExecution(org.springframework.test.context.TestContext)
afterTestClass
default void afterTestClass(TestContext testContext) throws Exception
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