Search code examples
javajunit

Are @After and @AfterClass guaranteed to run


Are @After and @AfterClass methods guaranteed to run even if there is a serious error or exception?


Solution

  • @After and @AfterClass annotated methods will be executed even if the test fail or a other (not catastrophic) exception occurs. There is only one Exception that skip the execution of the after methods: OutOfMemoryError ( org.apiguardian.api.API.BlacklistedExceptions).

    @After and @AfterClass are intended for tear down methods, that need to be executed after each/all tests, even if something went wrong.

    The code can be found in the method org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively()