In my Tests I have several Extensions via the *Callback Implementation.
(BeforeAllCallback
, BeforeEachCallback
, ....)
And I want to react on specific Exceptions from the implementation of the Callbacks. e.g. The Callback tries to report some teststatus to a TM-Tool. And I dont want the tests to fail, if the TM-Tool is not available.
So I found the LifecycleMethodExecutionExceptionHandler
and TestExecutionExceptionHandler
, but they are not invoked if an exception is thrown by a Lifecylce-Callback.
My TestClass:
@ExtendWith(ExceptionExtension.class)
@ExtendWith(MyExtension.class)
public class MyTest {
private static final Logger LOG = LoggerFactory.getLogger(MyTest.class);
@BeforeEach
void setup(){
LOG.info("called BeforeEach");
throw new RuntimeException("from BeforeEach");
}
@Test
void test() {
LOG.info("called Test");
throw new RuntimeException("from Test");
}
}
The Extension implementing the Callback:
public class MyExtension implements BeforeAllCallback {
private static final Logger LOG = LoggerFactory.getLogger(MyExtension.class);
@Override
public void beforeAll(ExtensionContext context) {
LOG.info("called BeforeEach Extension");
throw new RuntimeException("from BeforeEach Extension");
}
}
And the Extension Handling the Exceptions:
public class ExceptionExtension implements LifecycleMethodExecutionExceptionHandler, TestExecutionExceptionHandler {
private static final Logger LOG = LoggerFactory.getLogger(ExceptionExtension.class);
@Override
public void handleBeforeAllMethodExecutionException(ExtensionContext context, Throwable throwable) throws Throwable {
LOG.error("Exception handled:" + throwable.getMessage());
return;
}
@Override
public void handleBeforeEachMethodExecutionException(ExtensionContext context, Throwable throwable) throws Throwable {
LOG.error("Exception handled:" + throwable.getMessage());
return;
}
@Override
public void handleAfterEachMethodExecutionException(ExtensionContext context, Throwable throwable) throws Throwable {
LOG.error("Exception handled:" + throwable.getMessage());
return;
}
@Override
public void handleAfterAllMethodExecutionException(ExtensionContext context, Throwable throwable) throws Throwable {
LOG.error("Exception handled:" + throwable.getMessage());
return;
}
@Override
public void handleTestExecutionException(ExtensionContext context, Throwable throwable) throws Throwable {
LOG.error("Exception handled:" + throwable.getMessage());
return;
}
}
The Problem:
When Execution the code as shown above, the Exception from MyExtension
is not handled by the LifecycleMethodExecutionExceptionHandler
.
I receive the following output and the tests is aborted:
MyExtension - called BeforeEach Extension
If MyExtension
were not to throw an Exception - everything works as expected:
MyExtension - called BeforeEach Extension
MyTest - called BeforeEach
ExceptionExtension - Exception handled:from BeforeEach
MyTest - called Test
ExceptionExtension - Exception handled:from Test
What am I missing?