Will the writer.close()
method inside the finally { }
block run on an Junit Assertion Error?
Assume the following code:
@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?
}
}
Now will the finally()
block run just like a regular flow?
Yes, the
finally
block will run. Junit assertion errors are just normal exceptions so the usual javatry-catch-finally
pattern will work. You can even catch theAssertionError
exception if you wanted.