You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Let's assume I need to load and initialize some properties in the class ones for all tests and nested tests. I can do this in the class constructor when @TestInstance(Lifecycle.PER_CLASS) or in static-block. @BeforeAll lifecycle method is not applicable for this purpose, because it is invoked one time for main class and so many times how many nested (@Nested) classes we have, even when @TestInstance(Lifecycle.PER_CLASS).
Since it's bad practice to include the code potentially throwing exception in static-block, we will not load and initialize properties in static-block.
Only solution is to load and initialize properties in the class Constructor. And, of course, I need to log in my report any exceptions thrown during running test class including exceptions thrown in the class constructor. Moreover I need to capture actual state of application when this exception is thrown, like it has been handled in this feature request #1454.
Unfortunately I didn't find exception handler for the exceptions thrown in the class Constructor. I was looking for it in org.junit.jupiter.api.extension package, because other lifecycle methods exception handlers are located there.
Proposal
Introduce additional exception handler APIs which will be invoked for exceptions thrown in the class constructor. This make it possible to handle exceptions and take application screenshots at the correct point in time.
sbrannen
changed the title
Introduce extension APIs to handle exceptions from test class constructors
Introduce extension API to handle exceptions from test class constructors
Sep 20, 2022
Hi @marcphilipp , thank you for quick solution. I just tried to implement a InvocationInterceptor with overridden interceptTestClassConstructor with a try-catch, and it definitely works for me.
Will you consider introduction of separate exception handler for the exceptions thrown in the test class constructor similar to standard approach with exception handlers for other lifecycle methods?
Overview
Let's assume I need to load and initialize some properties in the class ones for all tests and nested tests. I can do this in the class constructor when
@TestInstance(Lifecycle.PER_CLASS)
or in static-block.@BeforeAll
lifecycle method is not applicable for this purpose, because it is invoked one time for main class and so many times how many nested (@Nested
) classes we have, even when@TestInstance(Lifecycle.PER_CLASS)
.Since it's bad practice to include the code potentially throwing exception in static-block, we will not load and initialize properties in static-block.
Only solution is to load and initialize properties in the class Constructor. And, of course, I need to log in my report any exceptions thrown during running test class including exceptions thrown in the class constructor. Moreover I need to capture actual state of application when this exception is thrown, like it has been handled in this feature request #1454.
Unfortunately I didn't find exception handler for the exceptions thrown in the class Constructor. I was looking for it in
org.junit.jupiter.api.extension
package, because other lifecycle methods exception handlers are located there.Proposal
Introduce additional exception handler APIs which will be invoked for exceptions thrown in the class constructor. This make it possible to handle exceptions and take application screenshots at the correct point in time.
Test class example:
And
LoggerExtension
will look like below:Deliverables
The text was updated successfully, but these errors were encountered: