Mockito test a void method throws an exception(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 方法抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Mockito 测试 void 方法抛出异常


基础教程推荐
- 不推荐使用 Api 注释的描述 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 从 python 访问 JVM 2022-01-01
- 大摇大摆的枚举 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01