How can I run a function or a wrap-up code after a test failed in JUnit 5?
This can be done using TestWatcher in JUnit 5. On JUnit 4 however, you can use ErrorCollector.
This answer brilliantly provides a solution to the problem.