JUnit:可以“预期” 一个包装的异常?
我知道可以在 JUnit 中定义一个 'expected' 异常,这样做:
@Test(expect=MyException.class)
public void someMethod() { ... }
但是如果总是抛出相同的异常,但具有不同的“嵌套”怎么办? 原因。
有什么建议么?
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.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
从 JUnit 4.11 开始,您可以使用
ExpectedException
规则的expectCause()
方法:As of JUnit 4.11 you can use the
ExpectedException
rule'sexpectCause()
method:您可以将测试代码包装在 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,您可以扩展默认测试运行程序来为您处理此问题(无需将每个方法包装在 try/catch 块中)
ExtendedTestRunner.java - 新测试运行程序:
ExtendedTest.java -用于标记测试方法的注释:
ExpectCauseException.java - 新的 JUnit 语句:
用法:
If you're using the latest version of JUnit you can extend the default test runner to handle this for you (without having to wrap each of your methods in a try/catch block)
ExtendedTestRunner.java - New test runner:
ExtendedTest.java - annotation to mark test methods with:
ExpectCauseException.java - new JUnit Statement:
Usage:
您始终可以手动执行此操作:
You could always do it manually:
您可以为异常创建 Matcher 。 即使您使用另一个测试运行器(例如 Arquillian 的
@RunWith(Arquillian.class) 因此您不能使用上面建议的
@RunWith(ExtendedTestRunner.class)
方法。这是一个简单的示例:
然后将其与 @Rule 一起使用和 ExpectedException 像这样:
添加由 Craig Ringer 在 2012 年编辑:增强且更可靠的版本:
布尔重新抛出
来抛出不匹配的异常。 这会保留嵌套异常的堆栈跟踪,以便于调试。@SaveVarargs
。完整代码:
典型输出:
You could create a Matcher for exceptions. This works even when you are using another test runner like Arquillian's
@RunWith(Arquillian.class)
so you can't use the@RunWith(ExtendedTestRunner.class)
approach suggested above.Here's a simple example:
Then use it with @Rule and ExpectedException like this:
Added by Craig Ringer in 2012 edit: An enhanced and more reliable version:
boolean rethrow
to throw unmatched exception. That preserves the stack trace of the nested exceptions for easier debugging.@SaveVarargs
on older versions.Full code:
Typical output:
为此,我编写了一个 JUnit 扩展。 静态辅助函数采用函数体和预期异常数组:
您现在可以像这样检查预期嵌套异常:
这可以节省您一次又一次编写相同的样板代码。
I wrote a little JUnit extension for that purpose. A static helper function takes a function body and an array of expected exceptions:
You can now check for expected nested exceptions like so:
This saves you writing the same boiler plate code again and again.
在 JUnit5 中,您可以使用assertThrows 方法,该方法除了断言抛出的异常之外,还返回该异常,以便您可以对其执行其他断言。
In JUnit5 you can use
assertThrows
method which apart of asserting the thrown exception it also returns it so that you can perform additional assertions on it.catch-exception 提供了最简洁的语法:
The most concise syntax is provided by catch-exception:
对于所有这些类型的断言,我们在assertj 中有asserThatTownBy。
We have asserThatThownBy in assertj for all these type of assertions.