Kay
Kay

Reputation: 1485

Are @After and @AfterClass guaranteed to run

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

Upvotes: 0

Views: 758

Answers (2)

Ralph
Ralph

Reputation: 120791

@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()

Upvotes: 2

Eugene
Eugene

Reputation: 5985

According to documentation:

All @After methods are guaranteed to run even if a Before or Test method throws an exception

All @AfterClass methods are guaranteed to run even if a BeforeClass method throws an exception

@After and @AfterClass are guaranteed to run.

Upvotes: 3

Related Questions