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

查看:38
本文介绍了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天全站免登陆