Junit 5:如何断言一个例外?
是否有更好的方法可以断言一种方法在Junit 5中引发了例外?
当前,我必须使用@rule
才能验证我的测试会引发异常,但是对于我期望多种方法在测试中引发异常的情况下,这不起作用。
Is there a better way to assert that a method throws an exception in JUnit 5?
Currently, I have to use a @Rule
in order to verify that my test throws an exception, but this doesn't work for the cases where I expect multiple methods to throw exceptions in my test.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
您可以使用
assertthrows()
,它允许您在同一测试中测试多个异常。在Java 8中支持Lambdas的支持下,这是测试Junit例外的规范方法。per junit docs :
You can use
assertThrows()
, which allows you to test multiple exceptions within the same test. With support for lambdas in Java 8, this is the canonical way to test for exceptions in JUnit.Per the JUnit docs:
在Java 8和Junit 5(木星)中,我们可以主张例外,如下所示。
使用
org.junit.jupiter.api.assertions.assertthrows
该方法将在
org.junit.jupiter.api
中使用功能界面可执行
。请参阅:
In Java 8 and JUnit 5 (Jupiter) we can assert for exceptions as follows.
Using
org.junit.jupiter.api.Assertions.assertThrows
That approach will use the Functional Interface
Executable
inorg.junit.jupiter.api
.Refer :
tl; dr:如果您在Junit 5.8.0+版本上,则应使用
AssertThrowSactly()
而不是assertthrows()
匹配确切的异常类型。您可以使用
assertthrows()
,但是使用assertthrows
即使抛出的例外是儿童类型,您的断言也会通过。这是因为,junit 5通过调用
class.isintance(..)
,class.isinstance(..)
也将返回true,即使投掷异常是儿童类型。解决方法是在课堂上断言:
TL;DR: If you are on JUnit 5.8.0+ version, you should use
assertThrowsExactly()
instead ofassertThrows()
to match the exact exception type.You can use
assertThrows()
, But withassertThrows
your assertion will pass even if the thrown exception is of child type.This is because, JUnit 5 checks exception type by calling
Class.isIntance(..)
,Class.isInstance(..)
will return true even if the thrown exception is of a child type.The workaround for this is to assert on Class:
他们在Junit 5中更改了它(预期:InvealArgumentException,实际:调用方法),并且代码看起来像这样:
They've changed it in JUnit 5 (expected: InvalidArgumentException, actual: invoked method) and code looks like this one:
现在,junit5提供了一种断言例外的方法,
您可以同时测试一般异常和定制异常
一个一般的例子:
greengegeneralexception.java
greengeneralexceptiontest.java.java
您可以找到一个用于测试样品测试的样品CustomeXception在这里:断言异常代码样本
ExpectcustomeXception.java
greenge customexceptiontest.java
Now Junit5 provides a way to assert the exceptions
You can test both general exceptions and customized exceptions
A general exception scenario:
ExpectGeneralException.java
ExpectGeneralExceptionTest.java
You can find a sample to test CustomException here : assert exception code sample
ExpectCustomException.java
ExpectCustomExceptionTest.java
您可以使用
assertthrows()
。我的示例取自docs http://junit.org/junit.org/junit5/junit5/junit5/docs/current5/current5 /用户指导/You can use
assertThrows()
. My example is taken from the docs http://junit.org/junit5/docs/current/user-guide/我认为这是一个更简单的示例,
呼叫
get()
包含一个空arrayList
的可选将抛出nosuchelementException
。Assertthrows
声明了预期的异常并提供了lambda供应商(不需要参数并返回值)。感谢@prime的回答,我希望得到详细说明。
I think this is an even simpler example
Calling
get()
on an optional containing an emptyArrayList
will throw aNoSuchElementException
.assertThrows
declares the expected exception and provides a lambda supplier (takes no arguments and returns a value).Thanks to @prime for his answer which I hopefully elaborated on.
一个更简单的衬里。使用Java 8和Junit 5,本示例不需要lambda表达式或卷发括号
An even simpler one liner. No lambda expressions or curly braces required for this example using Java 8 and JUnit 5
我的解决方案:
您可以这样称呼:
My solution:
And you can call it like this:
这是我在测试时要做的,以确保已抛出异常
This is what I do when testing to make sure an exception has been thrown
实际上,我认为该特定示例的文档中存在错误。预期的方法是
Actually I think there is a error in the documentation for this particular example. The method that is intended is expectThrows
如 user1075613 :
,并且还会生成拟合错误消息。 示例:
As mentioned in the comment by user1075613:
and it also generates fitting error message. Example:
这是一种简单的方法。
它只有在您期望抛出的例外情况下才能成功。
Here is an easy way.
It only succeeds when the Exception you expect is thrown.