使用 Mockito 从模拟中抛出已检查的异常
我试图让我的模拟对象之一在调用特定方法时抛出已检查的异常。我正在尝试以下操作。
@Test(expectedExceptions = SomeException.class)
public void throwCheckedException() {
List<String> list = mock(List.class);
when(list.get(0)).thenThrow(new SomeException());
String test = list.get(0);
}
public class SomeException extends Exception {
}
但是,这会产生以下错误。
org.testng.TestException:
Expected exception com.testing.MockitoCheckedExceptions$SomeException but got org.mockito.exceptions.base.MockitoException:
Checked exception is invalid for this method!
Invalid: com.testing.MockitoCheckedExceptions$SomeException
查看 Mockito 文档,他们只使用 RuntimeException,是不是不可能用 Mockito 从模拟对象中抛出已检查的异常?
I'm trying to have one of my mocked objects throw a checked Exception when a particular method is called. I'm trying the following.
@Test(expectedExceptions = SomeException.class)
public void throwCheckedException() {
List<String> list = mock(List.class);
when(list.get(0)).thenThrow(new SomeException());
String test = list.get(0);
}
public class SomeException extends Exception {
}
However, that produces the following error.
org.testng.TestException:
Expected exception com.testing.MockitoCheckedExceptions$SomeException but got org.mockito.exceptions.base.MockitoException:
Checked exception is invalid for this method!
Invalid: com.testing.MockitoCheckedExceptions$SomeException
Looking at the Mockito documentation, they only use RuntimeException
, is it not possible to throw checked Exceptions from a mock object with Mockito?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
检查 Java API 中的 列表。
get(int index)
方法声明为仅抛出扩展RuntimeException
的IndexOutOfBoundException
。您试图告诉 Mockito 抛出异常
SomeException()
,该异常无法由该特定方法调用抛出。进一步澄清。
List 接口不提供
get(int index)
方法抛出一个已检查的异常,这就是 Mockito 失败的原因。当您创建模拟列表时,Mockito 将使用List 的定义.class 来创建其模拟。
您使用
when(list.get(0)).thenThrow(new SomeException())
指定的行为 与 List API 中的方法签名不匹配,因为get(int index)
方法不会抛出SomeException()
因此 Mockito 失败。如果你真的想这样做,那么让 Mockito 抛出一个 new RuntimeException() 甚至更好地抛出一个 new ArrayIndexOutOfBoundsException() 因为 API 指定这是唯一有效的要抛出的异常。
Check the Java API for List.
The
get(int index)
method is declared to throw only theIndexOutOfBoundException
which extendsRuntimeException
.You are trying to tell Mockito to throw an exception
SomeException()
that is not valid to be thrown by that particular method call.To clarify further.
The List interface does not provide for a checked Exception to be thrown from the
get(int index)
method and that is why Mockito is failing.When you create the mocked List, Mockito will use the definition of List.class to creates its mock.
The behavior you are specifying with the
when(list.get(0)).thenThrow(new SomeException())
doesn't match the method signature in List API, becauseget(int index)
method does not throwSomeException()
so Mockito fails.If you really want to do this, then have Mockito throw a
new RuntimeException()
or even better throw anew ArrayIndexOutOfBoundsException()
since the API specifies that that is the only valid Exception to be thrown.解决方法是使用
willAnswer()
方法。以下操作(并且不会抛出
MockitoException
但实际上根据此处的要求抛出已检查的Exception
):例如,使用
BDDMockito
可以执行 简单的 Mockito 将使用doAnswer
方法A workaround is to use a
willAnswer()
method.For example the following works (and doesn't throw a
MockitoException
but actually throws a checkedException
as required here) usingBDDMockito
:The equivalent for plain Mockito would to use the
doAnswer
methodKotlin 有一个解决方案:
给出的来自哪里
There is the solution with Kotlin :
Where given comes from
请注意,一般来说,Mockito 确实允许抛出已检查的异常,只要异常在消息签名中声明即可。例如,假设
这样写是合法的:
但是,如果您抛出一个未在方法签名中声明的检查异常,例如
Mockito 将在运行时失败,并显示一些误导性的通用消息:
这可能会让您相信检查异常通常是不受支持,但实际上 Mockito 只是试图告诉您此已检查异常对于此方法无效。
Note that in general, Mockito does allow throwing checked exceptions so long as the exception is declared in the message signature. For instance, given
it's legal to write:
However, if you throw a checked exception not declared in the method signature, e.g.
Mockito will fail at runtime with the somewhat misleading, generic message:
This may lead you to believe that checked exceptions in general are unsupported, but in fact Mockito is only trying to tell you that this checked exception isn't valid for this method.
这在 Kotlin 中对我有用:
注意:抛出除 Exception() 之外的任何定义的异常
This works for me in Kotlin:
Note : Throw any defined exception other than Exception()
对于mockito,你可以使用
For mockito, you can just use