Mockito 测试 void 方法抛出异常 [英] Mockito test a void method throws an exception

查看:56
本文介绍了Mockito 测试 void 方法抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回类型为 void 的方法.它也可以抛出一些异常,所以我想测试那些抛出的异常.所有尝试都以相同的原因失败:

I have a method with a void return type. It can also throw a number of exceptions so I'd like to test those exceptions being thrown. All attempts have failed with the same reason:

类型Stubber中的when(T)方法不适用于参数(void)

The method when(T) in the type Stubber is not applicable for the arguments (void)

有什么方法可以让方法抛出指定的异常吗?

Any ideas how I can get the method to throw a specified exception?

doThrow(new Exception()).when(mockedObject.methodReturningVoid(...));

推荐答案

括号位置不好.

你需要使用:

doThrow(new Exception()).when(mockedObject).methodReturningVoid(...);
                                          ^

使用:

doThrow(new Exception()).when(mockedObject.methodReturningVoid(...));
                                                                   ^

文档对此进行了解释

这篇关于Mockito 测试 void 方法抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆