使用 JUnit 4 注释测试多个异常
是否可以在单个 JUnit 单元测试中测试多个异常?我知道可以使用一个异常,例如
@Test(expected=IllegalStateException.class)
现在,如果我想测试另一个异常(例如 NullPointerException),可以在同一个注释、不同的注释中完成,还是需要完全编写另一个单元测试?
Is it possible to test for multiple exceptions in a single JUnit unit test? I know for a single exception one can use, for example
@Test(expected=IllegalStateException.class)
Now, if I want to test for another exception (say, NullPointerException), can this be done in the same annotation, a different annotation or do I need to write another unit test completely?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您确实希望测试做一件事,并对其进行测试。如果您不确定将引发哪个异常,那么对我来说这听起来不是一个好的测试。
例如(在伪代码中),
所以如果您想测试您的方法是否抛出 2 个不同的异常(对于 2 组输入),那么您将需要 2 个测试。
You really want the test to do one thing, and to test for that. If you're not sure as to which exception is going to be thrown, that doesn't sound like a good test to me.
e.g. (in pseudo-code)
so if you want to test that your method throws 2 different exceptions (for 2 sets of inputs), then you'll need 2 tests.
这对于注释来说是不可能的。
使用 JUnit 4.7,您可以使用新的
ExpectedException
规则更多信息请参阅
如果您无法更新到 JUnit 4.7 ,您必须编写以下形式的裸单元测试
}
This is not possible with the annotation.
With JUnit 4.7 you can use the new
ExpectedException
ruleMore see
If updating to JUnit 4.7 is not possible for you, you have to write a bare unit test of the form
}
虽然这对于 JUnit 4 来说是不可能的,但如果您切换到 TestNG,这是可能的,它允许您编写
Although this is not possible with JUnit 4, it is possible if you switch to TestNG, which allows you to write
使用catch-Exception:
Use catch-exception:
这将抛出所有可能的异常。
This will throw all possible exceptions.
您希望“预期”如何发挥作用?一个方法只能抛出一个异常。
您必须为该方法可能失败的每种方式编写不同的单元测试。因此,如果该方法合法地抛出两个异常,那么您需要设置两个测试来强制该方法抛出每个异常。
How would you expect to "expected"s to work? A method can only throw one exception.
You would have to write a different unit test for each way the method can fail. So if the method legitimately throw two exceptions then you need two tests set up to force the method of throwing each exception.
使测试尽可能简单和简短。 JUnit 测试的目的是仅测试一种简单的功能或一种单一的失败方式。
事实上,为了安全起见,您应该为每种可能的执行方式至少创建一个测试。
通常,这并不总是可能的,因为如果您有一个分析字符串的方法,那么可能的字符串组合有很多,您无法涵盖所有内容。
保持简短。
您可以轻松地为一种方法拥有 30-40 种测试方法...这真的很重要吗?
问候
Keep the tests as simple and short as possible. The intention of a JUnit-Test is to test only one simple functionality or one single way of failure.
Indeed, to be safe, you should create at least one test for every possible execution way.
Normally, this is not always possible because if you have a method that analyses a string, there are so many possible string combinations that you cannot cover everything.
Keep it short and simple.
You can have 30-40 testing methods for one single method easily... does it really matter?
Regards