Background
Why do you need something like this?
We have an integration test that maps an entire workflow. The individual steps are each implemented as separate test methods.
They are executed in a fixed order using an order annotation. Of course, we could also pack all steps into a single test method, in which case it would also be aborted at the first error. The disadvantage here, however, would be that we could only see the progress via logging.
By mapping each step as an individual test method, we can see directly in the development environment which step we are in. However, it makes no sense to execute the other test methods of the integration test if a previous step fails. In this case, we therefore want to skip the execution of the other integration test steps.
The challenge
To realize this, we naturally do not want to include the same code in every test method over and over again: Per Assumption Query whether an error has already occurred, intercept errors, set a flag for the assumption.
Solution
We therefore simply create our own small extension. In this we implement the interface BeforeEachCallback. As a result, we have also developed the beforeEach available. In this we can offer a Assumption which skips the current test method if an error flag is set. Now we have to set the error flag. To do this, we also implement the TestWatcher-interface. We can use this to monitor the test execution. We are only really interested in the methods testAborted and testFailed. Here we simply set the flag anyFailed on true.
Using the extension is very simple. In every unit test class where this behavior is desired, we can now simply use the extension with
@ExtendsWith add:
Baeldung - Article about JUnit 5 TestWatcher API



