finally { }
block 中的 writer.close()
方法会在 Junit 断言错误上运行吗?
假设以下代码:
@Test
public void testWriter() {
try {
writer.open();
final List<MyBean> myBeans = new ArrayList<ProfileBean>();
/** Add 2 beans to the myBeans List here. **/
final int beansWritten = writer.writeBeans(myBeans);
// Say this assertion error below is triggered
org.junit.Assert.assertEquals("Wrong number of beans written.", -1, profilesWritten);
} finally {
writer.close(); // will this block run?
}
}
现在 finally()
block 会像常规流程一样运行吗?
最佳答案
是的,finally
block 将运行。 Junit 断言错误只是正常的异常,因此通常的 java try-catch-finally
模式将起作用。如果需要,您甚至可以捕获 AssertionError
异常。
关于java - 即使在 Junit 测试从 with in 'finally' block 中抛出断言错误后, 'try' block 是否会触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26847237/