Class EventPublishingTestExecutionListener
- java.lang.Object
- org.springframework.test.context.support.AbstractTestExecutionListener
- org.springframework.test.context.event.EventPublishingTestExecutionListener
- All Implemented Interfaces:
Ordered
,TestExecutionListener
public class EventPublishingTestExecutionListener extends AbstractTestExecutionListener
TestExecutionListener
that publishes test execution events to theApplicationContext
for the currently executing test. Events are only published if theApplicationContext
has already been loaded.Supported Events
BeforeTestClassEvent
PrepareTestInstanceEvent
BeforeTestMethodEvent
BeforeTestExecutionEvent
AfterTestExecutionEvent
AfterTestMethodEvent
AfterTestClassEvent
These events may be consumed for various reasons, such as resetting mock beans or tracing test execution. One advantage of consuming test events rather than implementing a custom
TestExecutionListener
is that test events may be consumed by any Spring bean registered in the testApplicationContext
, and such beans may benefit directly from dependency injection and other features of theApplicationContext
. In contrast, aTestExecutionListener
is not a bean in theApplicationContext
.Exception Handling
By default, if a test event listener throws an exception while consuming a test event, that exception will propagate to the underlying testing framework in use. For example, if the consumption of a
BeforeTestMethodEvent
results in an exception, the corresponding test method will fail as a result of the exception. In contrast, if an asynchronous test event listener throws an exception, the exception will not propagate to the underlying testing framework. For further details on asynchronous exception handling, consult the class-level Javadoc for@EventListener
.Asynchronous Listeners
If you want a particular test event listener to process events asynchronously, you can use Spring's
@Async
support. For further details, consult the class-level Javadoc for@EventListener
.- Since:
- 5.2
- Author:
- Sam Brannen, Frank Scheffler
- See Also:
@BeforeTestClass
,@PrepareTestInstance
,@BeforeTestMethod
,@BeforeTestExecution
,@AfterTestExecution
,@AfterTestMethod
,@AfterTestClass
Field Summary
Fields inherited from interface org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
Constructor Summary
Constructors Constructor Description EventPublishingTestExecutionListener()
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
afterTestClass(TestContext testContext)
void
afterTestExecution(TestContext testContext)
void
afterTestMethod(TestContext testContext)
void
beforeTestClass(TestContext testContext)
void
beforeTestExecution(TestContext testContext)
void
beforeTestMethod(TestContext testContext)
int
getOrder()
Returns10000
.void
prepareTestInstance(TestContext testContext)
Constructor Detail
EventPublishingTestExecutionListener
public EventPublishingTestExecutionListener()
Method Detail
getOrder
public final int getOrder()
Returns10000
.- Specified by:
getOrder
in interfaceOrdered
- Overrides:
getOrder
in classAbstractTestExecutionListener
- Returns:
- the order value
- See Also:
Ordered.HIGHEST_PRECEDENCE
,Ordered.LOWEST_PRECEDENCE
beforeTestClass
public void beforeTestClass(TestContext testContext)
- Specified by:
beforeTestClass
in interfaceTestExecutionListener
- Overrides:
beforeTestClass
in classAbstractTestExecutionListener
- Parameters:
testContext
- the test context for the test; nevernull
prepareTestInstance
public void prepareTestInstance(TestContext testContext)
- Specified by:
prepareTestInstance
in interfaceTestExecutionListener
- Overrides:
prepareTestInstance
in classAbstractTestExecutionListener
- Parameters:
testContext
- the test context for the test; nevernull
beforeTestMethod
public void beforeTestMethod(TestContext testContext)
- Specified by:
beforeTestMethod
in interfaceTestExecutionListener
- Overrides:
beforeTestMethod
in classAbstractTestExecutionListener
- Parameters:
testContext
- the test context in which the test method will be executed; nevernull
- See Also:
TestExecutionListener.afterTestMethod(org.springframework.test.context.TestContext)
,TestExecutionListener.beforeTestExecution(org.springframework.test.context.TestContext)
,TestExecutionListener.afterTestExecution(org.springframework.test.context.TestContext)
beforeTestExecution
public void beforeTestExecution(TestContext testContext)
- Specified by:
beforeTestExecution
in interfaceTestExecutionListener
- Overrides:
beforeTestExecution
in classAbstractTestExecutionListener
- Parameters:
testContext
- the test context in which the test method will be executed; nevernull
- See Also:
TestExecutionListener.beforeTestMethod(org.springframework.test.context.TestContext)
,TestExecutionListener.afterTestMethod(org.springframework.test.context.TestContext)
,TestExecutionListener.afterTestExecution(org.springframework.test.context.TestContext)
afterTestExecution
public void afterTestExecution(TestContext testContext)
- Specified by:
afterTestExecution
in interfaceTestExecutionListener
- Overrides:
afterTestExecution
in classAbstractTestExecutionListener
- Parameters:
testContext
- the test context in which the test method will be executed; nevernull
- See Also:
TestExecutionListener.beforeTestMethod(org.springframework.test.context.TestContext)
,TestExecutionListener.afterTestMethod(org.springframework.test.context.TestContext)
,TestExecutionListener.beforeTestExecution(org.springframework.test.context.TestContext)
afterTestMethod
public void afterTestMethod(TestContext testContext)
- Specified by:
afterTestMethod
in interfaceTestExecutionListener
- Overrides:
afterTestMethod
in classAbstractTestExecutionListener
- Parameters:
testContext
- the test context in which the test method was executed; nevernull
- See Also:
TestExecutionListener.beforeTestMethod(org.springframework.test.context.TestContext)
,TestExecutionListener.beforeTestExecution(org.springframework.test.context.TestContext)
,TestExecutionListener.afterTestExecution(org.springframework.test.context.TestContext)
afterTestClass
public void afterTestClass(TestContext testContext)
- Specified by:
afterTestClass
in interfaceTestExecutionListener
- Overrides:
afterTestClass
in classAbstractTestExecutionListener
- Parameters:
testContext
- the test context for the test; nevernull