mockito - 在看似正确的代码中获取 org.mockito.exceptions.misusing.InvalidUseOfMatchersException
问题描述
我有这样的when
电话:
@Mock
private MyEventFactory myEventFactory;
@Mock
private MyEvent myEvent;
when(myEventFactory.createMyEvent(anyObject(),anyObject(),anyObject(),anyObject(),anyObject(),anyObject(),anyObject(),anyObject(),anyObject(),anyObject(),anyObject())).thenReturn(myEvent);
但是我得到一个例外
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Misplaced argument matcher detected here:
You cannot use argument matchers outside of verification or stubbing.
MyEventFactory 是一个接口
这里可能是什么问题?
解决方案
你有没有初始化模拟?查看发布的完整代码会有所帮助,但我怀疑您需要调用类似
MockitoAnnotations.initMocks(this);
在你运行你的when
声明之前。本文展示了一个示例:https ://howtodoinjava.com/mockito/mockito-mock-initmocks/
推荐阅读
- javascript - 使用 JavaScript 处理对同一服务器的多个异步请求的正确方法是什么?当前和预报天气数据 API 请求
- java - JQPL 选择内部选择
- c - Clang-Tidy: 'fscanf' 用于将字符串转换为整数值,但函数不会报告转换错误;考虑改用“strtol”
- swift - 如何动态创建自动续订订阅
- javascript - 在关闭浏览器选项卡之前运行一些 javascript 代码
- javascript - 如何将 html 文件发送到 GrammarApi
- reactjs - 如何在 Next.js 中设置多语言环境动态页面的 getStaticPaths
- botframework - 让意图识别器中断用户提示
- c# - 如何将 UIContextMenuInteraction 添加到 Xamarin 窗体上的按钮?
- c - 为什么返回类型为int64_t时返回值-1变成4294967295?