【发布时间】:2013-12-06 20:01:13
【问题描述】:
应用程序在某些 thatMethod() 中抛出 2 个不同的异常。我希望通过 JUnit 对其进行测试。
我可以使用 ExpectedException 和 @Rule 来搁置应用程序预期抛出的“自然”异常。 How do you assert that a certain exception is thrown in JUnit 4 tests? 正在解释这一点。
在我的情况下,我该怎么做 - 2 个或更多“自然”例外? ExpectedException 没有持有多个预期异常。
还有其他方法可以做到这一点——再次在How do you assert that a certain exception is thrown in JUnit 4 tests? 中解释。我想知道是否有办法做到这一点 通过使用 JUnit 的内置功能。
【问题讨论】:
-
你可以设置一个普通的parent异常类型,然后尝试利用parametrized JUnit tests。这仅适用于您正在测试一种方法(顺便说一句,确实应该如此)。不理想,但它仍然可以工作:我正在使用它来测试针对不同方法参数组合引发的相同异常,但无需设置单独的测试。
标签: exception junit expected-exception