JUnit4 和异常处理
由于无法访问 catch 块,以下代码无法编译。
我想编写“tues”函数并在许多其他单元测试函数中调用它。
这可能吗?如何实施?
private void catchException(boolean condition) {
try
{
assertTrue(condition);
}
catch (SomeException e)
{
fail("exception = " + e.getMessage());
}
}
谢谢!
The following code does not compile, becouse of unreachable catch block.
I want write "tues" function and call it in many other unit test functions.
Is that possible and how to implement that?
private void catchException(boolean condition) {
try
{
assertTrue(condition);
}
catch (SomeException e)
{
fail("exception = " + e.getMessage());
}
}
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
测试方法中无需捕获异常即可使其失败:
如果
methodUnderTest()
抛出SomeException
,则testMethod()
将失败。There is zero need to catch an exception within a test method to fail it:
If
SomeException
is thrown bymethodUnderTest()
,testMethod()
will fail.您的代码的问题是
assertTrue
不会抛出SomeException
,如果您用抛出该异常的函数替换它,它将编译。The problem with your code is that
assertTrue
does not throwSomeException
, if you substitute it with the function that does, it will compile.我不完全确定你在这里问什么,但你发布的代码有一些问题。
如果 try 中的代码可能会抛出与您正在捕获的类型相同的异常,则只能为受检查的异常使用 catch 块。由于检查异常必须在方法的 throws 子句中声明,因此您可以检查正在调用的任何方法是否抛出您正在捕获的异常类型。来自 上的 JUnit 文档assertTrue:
可以看到它没有抛出任何检查异常。
此外,在 JUnit 测试中的 catch 块中调用
fail()
并不是真正必要的。如果抛出未捕获的异常,单元测试将自动失败。I'm not entirely sure what you're asking here, but there are a couple things wrong with the code you posted.
You can only have a catch block for a checked exception if the code in the try can potentially throw an exception of the same type you are catching. Since checked exceptions must be declared in the throws clause of a method, you can check to see if any of the methods you are calling throw the exception types you are catching. From the JUnit documentation on assertTrue:
You can see it doesn't throw any checked exceptions.
Also, calling
fail()
in a catch block in a JUnit test is not really necessary. A unit test will automatically fail if an uncaught exception is thrown.只需将
throws WhateverException
添加到您的测试签名中,如果抛出异常,测试就会失败。Just add
throws WhateverException
to your test signature and the test will fail if an exception is thrown.实际上并不清楚您要通过代码实现什么目的。如果您希望在断言失败时在 jUnit 引发的异常中拥有格式良好的消息,请考虑以这种方式编写它:
这样,如果检查失败,jUnit 将打印您提供的消息。我强烈建议这样做,特别是如果您有大量测试,因为它
It is actually not really clear what you what to achieve with your code. If you want to have a nicely formatted message in the exception fired by jUnit if your assert fails then consider writing it in this way:
In this way jUnit will print the message you provided if the check fails. I highly recommend this, especially if you have lots of tests because it
通过
Exception
修改SomeException
将导致代码编译。Modifying
SomeException
byException
will cause the code to compile.显然我的问题不清楚。
我有很多单元测试,所有方法都抛出相同的异常和不同的错误消息。 “SomeException”是异常,我必须捕获并从中读取错误消息。
我想要实现的是编写一种方法,该方法对于所有单元测试都是通用的,并且可以在其中打印错误消息。
现在单元测试看起来像这样
所以下面的代码在所有单元测试中重复,这是不行的。
Obviously my question is not clear.
I have many unit tests and all methods are throwing same exception with different error message. "SomeException" is exception I must catch and read error message from it.
What I want to achive is to write one method which will be common to all unit tests and where I could print error message.
Now unit tests looks like this
So below code is repeating in all unit tests and that is not ok.