-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Intercept Exceptions in @BeforeEach for Handling before @AfterEach #3411
Comments
@fleytman |
If you would like us to be able to process this issue, please provide the requested information. If the information is not provided within the next 3 weeks, we will be unable to proceed and this issue will be closed. |
I think so, but I'll run a couple more experiments in the next couple weeks to be sure. |
Optimistically closing. @fleytman Please comment here if it turns out not to work for you after all. |
@marcphilipp thx, your method is working override fun handleBeforeEachMethodExecutionException(context: ExtensionContext?, throwable: Throwable?) {
println(context?.executionException?.toString())
println("handleBeforeEachMethodExecutionException")
super.handleBeforeEachMethodExecutionException(context, throwable)
} But i have a question why |
That's explained in the Javadoc for that method:
Since a That's why the exception to handle is passed directly to the |
Problem:
@BeforeEach
method and an inability to intercept them before calling the@AfterEach
method, even if an exception occurred in the test.@AfterTestExecutionCallback
extension allows handling exceptions that occur during the execution of the@Test
method (@Test
method). However, this extension doesn't work for the@BeforeEach
method, thus providing no means to intercept exceptions in this setup phase.I have a concrete example demonstrating the issue with my code and its output. You can find it here: Code Example and Output.
Consequences:
@BeforeEach
before calling the@AfterEach
method can lead to redundant logging and capturing screenshots. For instance, if the@AfterEach
method involves finalization activities, such as resetting the application or cleaning up data, logs and screenshots might be taken not at the moment of the failure but much later.Desired Solution:
Introduce a new extension or callback method in JUnit 5 that allows handling exceptions in both
@BeforeEach
and@Test
methods before calling@AfterEach
.This proposed solution would enable developers to react to exceptions from
@BeforeEach
and perform additional actions before calling@AfterEach
, enhancing the flexibility of managing resources and logic for each test scenario, as well as ensuring more reliable logging and capturing screenshots at the point of failure.By implementing this enhancement, JUnit would become an even more powerful tool for developing and managing test scenarios, providing improved error handling, and simplifying the creation of reliable test suites.
The text was updated successfully, but these errors were encountered: