JUnit:可能“期望"一个包装异常? [英] JUnit: Possible to 'expect' a wrapped exception?

查看:18
本文介绍了JUnit:可能“期望"一个包装异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道可以在 JUnit 中定义一个 'expected' 异常,这样做:

I know that one can define an 'expected' exception in JUnit, doing:

@Test(expect=MyException.class)
public void someMethod() { ... }

但是如果总是抛出相同的异常,但具有不同的嵌套"怎么办原因.

But what if there is always same exception thrown, but with different 'nested' causes.

有什么建议吗?

推荐答案

您可以将测试代码包装在 try/catch 块中,捕获抛出的异常,检查内部原因,记录/断言/无论如何,然后重新抛出例外(如果需要).

You could wrap the testing code in a try / catch block, catch the thrown exception, check the internal cause, log / assert / whatever, and then rethrow the exception (if desired).

这篇关于JUnit:可能“期望"一个包装异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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