【问题标题】:How to use Mockito to test APIs along Robospice and Retrofit?如何使用 Mockito 测试 Robospice 和 Retrofit 的 API?
【发布时间】:2014-03-11 14:22:29
【问题描述】:

我一直在关注这个blog entry,它展示了如何使用 Mockito 和 Retrofit 模拟请求。问题是我在 Robospice 中同时使用两者,它不需要在服务接口上提供 Callback 作为参数(因为它将是 synchronous call):

@GET("/foo/bar")
User foo(@Query("bar") String baz);

所以我无法以这种方式拦截我的测试中的回调:

Mockito.verify(mockApi).repositories(Mockito.anyString(), cb.capture());
User user = new User();
cb.getValue().success(user, null);

有什么方法可以做到这一点吗?谢谢!

【问题讨论】:

    标签: android mockito android-testing robospice retrofit


    【解决方案1】:

    模拟服务接口,然后编写脚本以返回您想要的值。

    doReturn(new User()).when(service).foo(anyString());
    

    您可以稍后验证是否调用了此方法。

    verify(service).foo(anyString())
    

    【讨论】:

    • 模拟服务是什么意思?
    猜你喜欢
    • 2014-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-10
    • 2021-06-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多