我已在我的一个Project for Checkout目的中实现了Masterpass SDK . API用于通过传递CallBackUrl和OrginURL来获取请求令牌API已成功实现并且其工作正常 .
问题:我在使用Mockito创建junit测试用例时遇到问题 .
码:
public void invokeService(AuthTokenRequestBean requestBean) throws Exception {
retrieveAuthTokenService = new RetrieveAuthTokenService(requestBean);
when(processorFactory.createService(eq(RetrieveAuthTokenService.class),
any(AuthTokenRequestBean.class))).
thenReturn(retrieveAuthTokenService);
Mockito.when(retrieveAuthTokenService.getOutput()).thenReturn(response);
Mockito.when(retrieveAuthTokenService.process(any(FlowCtx.class))).thenReturn(response);
}
例外:
rg.mockito.exceptions.misusing.MissingMethodInvocationException:
when()需要一个必须是'模拟方法调用'的参数 . 例如:when(mock.getArticles()) . thenReturn(articles);
此外,此错误可能会显示,因为:1 . 您存根:final / private / equals()/ hashCode()方法 . 这些方法无法进行存根/验证 . 不支持在非公共父类上声明的模拟方法 . 2.在()内部,你不是在模拟上调用方法,而是在某些其他对象上调用方法 .
如果有人使用junit处理这些api进行模拟,那么需要帮助 . 是否有任何其他框架可用于模拟 .
1 回答
我们可以使用PowerMockito模拟MasterPass SDK实现 . 由于这包含静态方法,可以使用PowerMockito进行模拟 .
代码片段在下面..
@Mock私有ProcessorFactory处理器XXXXX;
public void setupAndXXXProcessor(AuthTokenRequestXXXX requestBean)抛出异常{retrieveAuthTokenProcessor = new RetrieveAuthXXXXXX(requestBean); PowerMockito.when(processorXXXX.createProcessor(eq(RetrieveAuthXXXXXX.class),any(AuthTokenRequestXXXX.class))).thenReturn(retrieveAuthTokenProcessor); PowerMockito.mockStatic(RequestTokenApi.class); . PowerMockito.when(RequestTokenApi.create(anyString()))thenReturn( “createRequestTokenResponse”); PowerMockito.mockStatic(ShoppingCartApi.class); PowerMockito.when(ShoppingCartApi.create(any(ShoppingCartRequest.class))) . thenReturn(createShoppingCartResponse()); PowerMockito.mockStatic(MerchantInitializationApi.class); PowerMockito.when(MerchantInitializationApi.create(any(MerchantInitializationRequest.class))) . thenReturn(“createMerchantInitializationResponse”);
}