@After 和@AfterClass 是否保证 运行

Are @After and @AfterClass guaranteed to run

即使出现严重错误或异常,@After 和@AfterClass 方法是否保证运行?

@After@AfterClass 注释的方法将被执行,即使测试失败或其他(非灾难性)异常发生。只有一个 Exception 会跳过 after 方法的执行:OutOfMemoryError ( org.apiguardian.api.API.BlacklistedExceptions).

@After@AfterClass 用于拆卸方法,需要在 each/all 测试后执行,即使出现问题。

代码可以在方法中找到org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively()

根据文档:

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@AfterClass 保证 运行.