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

标签 java unit-testing junit junit4 try-finally

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/

相关文章:

java - 从Java中的展平列表重建二叉搜索树?

java - 在Domino Notes中用java添加图片

java - PostgreSQL 和 JMS(或其他发布-订阅/回调机制)

java - Saxon 9.4 来自 cygwin 的查询

c++ - gtest,对 'testing::Test::~Test()' 的 undefined reference ,testing::Test::Test()

node.js - 作为 node express 中间件被跳过的 Sinon stub

python - 如果任何单元测试失败,如何使 Python 的覆盖工具失败?

junit - Assert.assertEquals junit 参数顺序

maven - Intellij-idea调试器在调试maven测试时断开连接

java - JUnit spring无法使用@Autowired注入(inject)位于其他项目中的相关bean