How prevent @AfterTest execution in @BeforeEach? Throwing an exception in @BeforeEach does not help: @AfterEach will execute anyway.
1 Answer
It’s the purpose of methods annotated with @AfterEach to be called regardless of what happens in a test. That’s why you cannot and should not prevent it. If you have the need to execute clean up code on a case by case basis you have several options:
- Structure your tests differently, eg using nested tests
- Make the after-each code more robust so that it can run anyway
- Run the after code directly in those tests where it applies