Junit5: Introduce extension APIs to handle exceptions from lifecycle callback methods

Created on 6 Jun 2018  路  10Comments  路  Source: junit-team/junit5

Overview

I need to handle exceptions thrown during the execution of each @BeforeAll, @AfterAll, @BeforeEach, and @AfterEach method.

There are test lifecycle callbacks. Just for error tracking needs these callbacks are useful, but if I need to capture AUT (application under test) state (for example to take an application screenshot, log current condition) at exception time, then these callbacks are not enough, because application state is changed before execution reaches the callback.

Let's say an exception occurs in a @BeforeEach method. I need to take an application screenshot at this exception. The nearest callback to handle the exception is AfterEachCallback. So application state is changed in @AfterEach method, and we lose original screen of application in AfterEachCallback. I reference to the diagram here.

Proposal

Introduce the following additional _callback_ extension APIs which will be invoked after each @BeforeAll, @AfterAll, @BeforeEach and @AfterEach method. This make it possible to handle exceptions and take application screenshots at the correct point in time.

  • AfterBeforeAllCallack
  • AfterAfterAllCallback
  • AfterBeforeEachCallback
  • AfterAfterEachCallback
Jupiter extensions new feature

All 10 comments

Tentatively slated for the 5.3 backlog for _team discussion_

Alternatively, we could add these callback methods to TestExecutionExceptionHandler, e.g. handleExceptionInBeforeEachMethod(ExtensionContext context, Throwable throwable).

Yep, that's also an option.

I'm also concerned about handling exceptions in time in constructors.
Introduction of additional callback methods or exception handlers (preferred) for constructors could be a good option as well taking into account @TestInstance values Lifecycle.PER_METHOD and Lifecycle.PER_CLASS.

I made PR #1509. It handles exceptions for BeforeEach and AfterEach methods. BeforeAll and AfterAll is still missing - it's just to get the idea.
@marcphilipp Is this what you had in mind?

@phoenix384 Yeah, that looks promising! Would you mind adding the same for @BeforeAll and @AfterAll?

@marcphilipp done

Tentatively slated for 5.3 RC1 due to work being performed in #1509.

As the work done on #1509 got stalled, I prepared another pull request #1868, that fixes the most outstanding issues, i.e. fulfills the DoD and has no conflicts with master. Appreciate the review.

Resolved in 2844ee40948b9ae14bc7426ad73c3400dbfff3a5

Was this page helpful?
0 / 5 - 0 ratings