gpt4 book ai didi

java - 即使在 Junit 测试从 with in 'finally' block 中抛出断言错误后, 'try' block 是否会触发?

转载 作者:搜寻专家 更新时间:2023-10-30 19:40:34 27 4
gpt4 key购买 nike

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com