The Spring TestContext Framework provides a TestExecutionListener
SPI that can be used to achieve this.
Basically, if you implement TestExecutionListener
(or better yet extend AbstractTestExecutionListener
), you can implement the afterTestMethod(TestContext)
method. From the TestContext
that is passed in you can access the exception that was thrown (if there is one).
Here's the Javadoc for org.springframework.test.context.TestContext.getTestException()
:
Get the exception that was thrown during execution of the test method.
Note: this is a mutable property.
Returns: the exception that was thrown, or null if no exception was thrown
FYI: you can register your customer listener via the @TestExecutionListeners
annotation.
Regards,
Sam