java - 如何使用 mockito 框架获取返回的对象
问题描述
Mockito 框架的新手。
RcExtensionInfo rcExtensionInfo = rcApiClient.get(
RC_OFFICE_EXTENSION_INFO_API_PATH,
pathVariablesForExtensionInfoRequest,
customRequestHeaders,
RcApplicationHeadersService.RcApplicationType.OFFICE_INTEGRATION,
RcExtensionInfo.class
);
上面的代码我需要用 mockito 测试,我想要真正的对象,RcExtensionInfo
因为这个对象在我的代码中用于少数 getter 方法。目前无法调用 getter 方法,因为null
. 我们知道,如果我们模拟某个方法,那么默认值就是null
. 但我认为我们可以将thenReturn
方法中的对象分配给该引用。
测试代码:
RcAccountInfo rcAccountInfo = new RcAccountInfo();
rcAccountInfo.setMainNumber("+198473621");
when(rcApiClient.get(RC_OFFICE_ACCOUNT_INFO_API_PATH,
new HashMap<String, String>(), new HashMap<String, String>(),
RcApplicationHeadersService.RcApplicationType.OFFICE_INTEGRATION,
RcAccountInfo.class)).thenReturn(rcAccountInfo);
解决方案
您可以调用Mockito.mock(YourClass.class, Mockito.CALLS_REAL_METHODS)
它,如果 没有配置任何其他内容when(..).then(...)
,则在模拟对象上调用真正的方法。
推荐阅读
- php - 如何在序列中的同一插入上插入序列号?
- bash - Bash单行嵌套for循环将比较变量作为命令
- python - Python pandas数据框'SUMIF' - 标准范围与总和范围不同
- c++ - 在OpenGL的角落显示文本
- node.js - 同步 node.js 调用
- javascript - 使用 JS(和 jquery 和/或 AJAX)在 HTML 中加载 LOCAL JSON 文件;答案=不可能
- java - Java CompletableFuture 分配执行器
- c# - iTextSharp 升级到 iText7 .NET
- javascript - 如何使 touchstart 和 touchend 和 touchover 与鼠标事件一起工作?
- webpack - 如何在 webpack4 manifest.json 中为 *ALL* .js、.css 和 .png 资产设置键前缀?