JUnit中有多个预期的异常 [英] Multiple expected exceptions in JUnit
问题描述
应用程序在thatMethod()中抛出2个不同的异常.我希望通过JUnit对其进行测试.
The application is throwing 2 different exceptions in some thatMethod(). I'm looking to test it by JUnit.
我可以使用 ExpectedException 和 @Rule 来搁置应用程序可能引发的自然"异常. 您如何断言在JUnit 4测试中抛出某个异常?对此进行了解释.
I can use ExpectedException and @Rule to set aside a "natural" exception the application is expected to throw. How do you assert that a certain exception is thrown in JUnit 4 tests? is explaining this.
在我的情况下,我该如何做-2个或更多"自然"例外? ExpectedException 不包含多个预期的异常.
How can i do that in my case-- 2 or more "natural" exceptions? ExpectedException is not holding multiple expected exceptions.
There are other ways to do this-- as explained in How do you assert that a certain exception is thrown in JUnit 4 tests? again. i'm wondering whether there is a way to do this by using the built-in features of JUnit.
推荐答案
单个测试方法只能退出一次,因此只能抛出一个异常.如果您希望确认代码可以通过两种不同的方式失败,则可以进行两个单独的测试.将其分为两个测试方法,并在每个方法上声明特定的异常.
A single test method can only exit once, and so can only throw a single exception. If you wish to confirm that your code can fail in two different ways then you have two separate tests; split it into two test methods and declare the specific exception on each.
这篇关于JUnit中有多个预期的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!