我需要验证对 的调用handler.sendMessage(msg)
。
代码:
//This bundleImportStorage will send message to UI handler by passing message object
bundleImporter.bundleImportFromStorage(intent);
//In this line I am getting error
when(uiHandler.sendMessage(any(Message.class))).thenReturn(true);
Run Code Online (Sandbox Code Playgroud)
异常详情:
//This bundleImportStorage will send message to UI handler by passing message object
bundleImporter.bundleImportFromStorage(intent);
//In this line I am getting error
when(uiHandler.sendMessage(any(Message.class))).thenReturn(true);
Run Code Online (Sandbox Code Playgroud)
根据消息“预期 2 个匹配器,记录 1 个”,如果UiHandler.sendMessage(Message)
最终方法委托给非最终的双参数方法(通常是同名的重载),则可能会发生这种情况。这是一个问题,因为 Mockito 坚持每个参数都只有一个匹配器,如果使用了任何匹配器。因为 final 方法是由静态 dispatch调用的,并且因为 Mockito 通过动态覆盖模拟类来工作,所以 Mockito 的第一个交互是使用两个参数的方法,它假设您正在使用单个匹配器进行存根(解释否则会引起混淆的错误消息) .
如果可行,请确保 UiHandler 和 sendMessage 都是公开的和非最终的。如果没有,您可能需要求助于Robolectric或PowerMockfinal
在字节码级别覆盖该方法。
我已经写了更多关于 Mockito 内部结构的文章,包括每个参数需要一个匹配器,在 SO:Mockito 匹配器如何工作?
归档时间: |
|
查看次数: |
4411 次 |
最近记录: |