gpt4 book ai didi

java - 如何使用mockito框架获取返回的对象

转载 作者:行者123 更新时间:2023-12-01 22:11:24 24 4
gpt4 key购买 nike

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(... ),在模拟对象上调用真正的方法。

关于java - 如何使用mockito框架获取返回的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58659516/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com