即使在Junit测试中从"try"块中引发断言错误后,"finally"块也会触发吗? [英] Will the 'finally' block fire even after a Junit test throws an Assertion Error from with in 'try' block?

查看:123
本文介绍了即使在Junit测试中从"try"块中引发断言错误后,"finally"块也会触发吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

finally { }块中的writer.close()方法是否会在Junit断言错误上运行?

Will the writer.close() method inside the finally { } block run on an Junit Assertion Error?

假设以下代码:

@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()块会像常规流一样运行吗?

Now will the finally() block run just like a regular flow?

推荐答案

是的,finally块将运行. Junit断言错误只是正常的异常,因此通常的java try-catch-finally模式将起作用.如果需要,您甚至可以捕获AssertionError异常.

Yes, the finally block will run. Junit assertion errors are just normal exceptions so the usual java try-catch-finally pattern will work. You can even catch the AssertionError exception if you wanted.

这篇关于即使在Junit测试中从"try"块中引发断言错误后,"finally"块也会触发吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆