我写了一些自动化测试,我使用的语法如下 –
try { // Test case steps and validations } finally { // Clean up related to test }
注意:这没有catch块,因为我的测试不期望异常.
如果try和finally块中的测试失败,则只在控制台上返回finally的失败而不是try.这里的简单示例(此处使用TestNG进行断言) –
try { Assert.assertEquals(true,false,"Boolean values did not match"); } finally { Assert.assertEquals(100,10,"Integer values did not match"); }
在这种情况下,仅返回最终失败的详细信息.
这无助于识别查看控制台的测试的实际失败.
我想了解为什么Java不会在控制台上返回两个故障细节,这可以帮助用户在第一眼看到故障原因.
解决方法
抛出异常可以观察到同样的事情:
try { throw new RuntimeException("Message 1"); } finally { throw new RuntimeException("Message 2"); }
这里只打印消息2:
Exception in thread "main" java.lang.RuntimeException: Message 2
这是因为当最终抛出异常时,try中的任何异常都被“丢弃并遗忘”:
JLS第14.20.2节
If the run-time type of V is not assignment compatible with a catchable exception class of any catch clause of the try statement,
then the finally block is executed. Then there is a choice:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。