在 JUnit 中,@After 方法 运行 会在 @Test 方法抛出未处理的异常时发生吗?

In JUnit, will @After method run, when @Test method throws an exception which is unhandled?

即使 @Test 方法抛出未处理的异常或者这实际上是由 JUnit 在内部完成的,我如何确保我的 @After 方法运行?

是的,@After 方法总是 运行,即使在 @Test 方法中抛出异常也是如此。

如果未配置 @Test(expected=ExceptionClass.class),测试将失败。

JUnit 在每个测试用例之后运行带有 @After 注释的方法,无论是否抛出异常。

引用the JUnit documentation:

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