【发布时间】:2019-11-01 12:49:26
【问题描述】:
Mockito 框架的新手。
RcExtensionInfo rcExtensionInfo = rcApiClient.get(
RC_OFFICE_EXTENSION_INFO_API_PATH,
pathVariablesForExtensionInfoRequest,
customRequestHeaders,
RcApplicationHeadersService.RcApplicationType.OFFICE_INTEGRATION,
RcExtensionInfo.class
);
上面的代码我需要用 mockito 测试,我想要RcExtensionInfo 的真实对象,因为这个对象在我的代码中用于少数 getter 方法。由于null,目前无法调用getter 方法。据我们所知
如果我们模拟一些方法,那么默认值将是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);
【问题讨论】: