检查 Junit 中的 2 个期望值
我有一个 java 程序,它针对 2 个不同的场景抛出带有 2 条不同消息的异常,并且我希望 Junit 测试用例检查这两条消息是否相等。举个例子 -
public void amethod() {
// do some processing
if(scenario1 == true) {
throw new MySystemException("An error occured due to case 1 being incorrect.");
}
else if(scenario2 == true) {
throw new MySystemException("An error occured as case 2 could not be found");
}
}
现在这个 JUnit 会是这样的 -
public void testAMethod() {
// do something
assertEquals("Expected", "Actual");
}
据我所知,在上面的例子中,如果我使用 Scenario1
异常消息,当抛出 异常时,junit 将失败场景2
,反之亦然。
我想知道 Junit 中是否提供了任何其他方式可以使用这个测试方法
并检查两条消息以使测试通过?
类似 OR
的东西,如果可能的话,提供带有这些预期消息的“预期”值。
我希望我的查询足够清楚。
谢谢
更新
很抱歉回复延迟,因为有其他紧急事务。
谢谢大家提出的非常好的建议,它确实帮助我现在更好地理解了。
最终,为了保持简单,我决定实施 Don Roby 建议的有点类似的解决方案。因此创建了一个新的测试类,如下所示 -
public void testAMethodScenario1() {
// do the necessary
assertEquals("Expected Exception Message 1", "Actual");
}
public void testAMethodScenario2() {
// do the necessary
assertEquals("Expected Exception Message 2", "Actual");
}
再次感谢大家的回复。
I have a java program which throws an exception with 2 different messages for 2 different scenarios and I want the Junit test case to check for equality for both of these messages. As an example -
public void amethod() {
// do some processing
if(scenario1 == true) {
throw new MySystemException("An error occured due to case 1 being incorrect.");
}
else if(scenario2 == true) {
throw new MySystemException("An error occured as case 2 could not be found");
}
}
Now the JUnit for this would be something like-
public void testAMethod() {
// do something
assertEquals("Expected", "Actual");
}
As I understand, in this above example, if I use the Scenario1
exception message the junit will fail when an exception is thrown for Scenario2
and vice versa.
I would like to know if there is any other way provided in Junit by which I can use this one test method
and check for both the messages for the test to pass?
Something like an OR
, if possible to provide the "Expected" value with both these expected message.
I hope my query is clear enough.
Thanks
UPDATE
Sorry for the delayed response, had got caught up with some other urgent matter.
Thank you all for the very nice suggestions, it certainly has helped me to understand a bit better now.
Eventually, to keep it rather simple I decided to implement a somewhat similar solution suggested by Don Roby. So created a new test class which looks like -
public void testAMethodScenario1() {
// do the necessary
assertEquals("Expected Exception Message 1", "Actual");
}
public void testAMethodScenario2() {
// do the necessary
assertEquals("Expected Exception Message 2", "Actual");
}
Thank you all again for your responses.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我认为您需要手动捕获异常(对于每个场景)并单独检查消息:
当然,您可以将这些场景定义为枚举常量,然后简单地迭代它们并在循环中检查每个场景,因为“复制/粘贴设计模式”在上面的代码中非常明显。 :)
I think you need to manually catch the exception (for each scenario) and individually check the message:
Of course, you can have these scenarios defined as enum constants and simply iterate through them and check each of them within a loop, since the "copy/paste design pattern" is pretty obvious in the above code. :)
您似乎在这里问两件事,如何测试异常以及如何断言一个值与两个可能的期望值中的一个匹配。
要测试异常,您可以使用 JUnit4 注释:
或在测试中使用 try-catch:
如果您只有一条消息,在 try-catch 版本中,您可以使用
断言已获取该消息catch 块中的 AssertEquals
。最好的测试将对您的两个场景进行单独的测试,并期望得到正确的单个消息。事实上,更好的代码对于这两种情况可能有不同的例外。
但无论如何,确实需要比简单相等更复杂的断言,并且 中有一个优雅的解决方案。 Hamcrest 匹配器。
在这种情况下使用它,您可以编写类似的内容(未经测试 - 不要完全相信我的语法):
You seem to be asking two things here, how to test an exception and how to assert that a value matches either of two possible expected values.
To test for an exception, you can either use a JUnit4 annotation:
or use a try-catch in your test:
And if you have only one message, in the try-catch version you can assert that you got it with an
AssertEquals
in the catch block.The best testing would have separate tests for your two scenarios, and expect the correct single message. Better code might in fact have distinct exceptions for the two situations.
But the need for a more complex assertion than simple equality does come up anyway, and there's an elegant solution for it in Hamcrest matchers.
Using that for this situation, you could write something like (untested - don't trust my syntax completely):
你能预测会发生哪种情况吗?如果是这样,科斯蒂的答案就是正确的。如果没有,因为存在一些随机性或其他什么原因,你可以写:
Can you predict which scenario will occur? If so, Costi's answer is correct. If not, because there's some randomness or whatever, you can write:
方法抛出的异常的声明类型是其 API 的一部分。如果您确实想要区分不同的故障模式,则应该为每种故障模式声明不同的异常类型。
所以,像这样:
The declared types of exception thrown bya method are part of its API. If you really want to distinguish different failure modes, you should declare a different exception type for each failure mode.
So, something like this:
JUnit4 中的 @Rule 解决方案:
在您的测试用例中,使用规则:
@Rule solution in JUnit4:
In your testcase, use the Rule:
JUnit 4 提供了 (Expected Exception.class)
Google: Expected Exceptions JUnit 了解更多信息。
JUnit 4 provides (Expected Exception.class)
Google: Expected Exceptions JUnit for more info.
BDD 风格解决方案,采用 捕获异常
源代码
依赖项
BDD Style Solution with Catch Exception
Source code
Dependencies
使用 @Rule 是一个更好的解决方案,您还可以断言异常和期望消息。
@Rule 是编写异常的更优雅的方式。
A better solution with @Rule, you can assert both exception and expection message as well.
@Rule is the more elegant way to write the exception.