【问题标题】:Using Mockito with Retrofit 2.0在 Retrofit 2.0 中使用 Mockito
【发布时间】:2016-02-21 07:05:18
【问题描述】:

我正在尝试使用 Mockito 为我的 api 调用(通过 Retrofit 2.0 进行)创建单元测试。

这似乎是最流行的关于使用MockitoRetrofit 的博客。

http://mdswanson.com/blog/2013/12/16/reliable-android-http-testing-with-retrofit-and-mockito.html

不幸的是,它使用了早期版本的Retrofit,并依赖于CallbacksRetrofitError,它们从2.0 中停止。

Retrofit 2.0 如何做到这一点?

P.S.:我正在使用 RxJavaretrofit,所以与 RxJava 一起使用的东西会很棒。谢谢!

【问题讨论】:

  • 你看过 OkHttp 的 MockWebServer 和 MockResponse 类吗?他们可以在没有 Mockito 依赖的情况下完成所有这些......
  • 您可以在改造中使用拦截器创建模拟服务器以进行单元测试

标签: android mockito retrofit rx-java android-testing


【解决方案1】:

在 Retrofit 的官方存储库中有一个有用的示例: https://github.com/square/retrofit/tree/master/retrofit-mock

我还发现:https://touk.pl/blog/2014/02/26/mock-retrofit-using-dagger-and-mockito/

你会在这里找到这个片段:

单元测试

在应用程序开发过程中,您可以随时向服务器发送请求(或 大多数时候)所以可以在没有模拟服务器的情况下生活,它 很烂,但有可能。可惜你写的不好 没有模拟的测试。下面有两个单元测试。其实他们 不测试任何东西,但以简单的方式展示如何模拟Retrofit 使用MockitoDagger 提供服务。

    @RunWith(RobolectricTestRunner.class)
public class EchoServiceTest {

    @Inject
    protected EchoService loginService;

    @Inject
    protected Client client;

    @Before
    public void setUp() throws Exception {
        Injector.add(new AndroidModule(), 
                     new RestServicesModule(),
                     new RestServicesMockModule(),
                     new TestModule());
        Injector.inject(this);
    }

    @Test
    public void shouldReturnOfferInAsyncMode() throws IOException {
        //given
        int expectedQuantity = 765;
        String responseContent = "{" +
                "   \"message\": \"mock message\"," +
                "   \"quantity\": \"" + expectedQuantity + "\"" +
                "}";
        mockResponseWithCodeAndContent(200, responseContent);

        //when
        EchoResponse echoResponse = loginService.getMessageAndQuantity("test", "test");

        //then
        assertThat(echoResponse.getQuantity()).isEqualTo(expectedQuantity);
    }

    @Test
    public void shouldReturnOfferInAsyncModea() throws IOException {
        //given
        int expectedQuantity = 2;
        String responseContent = "{" +
                "   \"message\": \"mock message\"," +
                "   \"quantity\": \"" + expectedQuantity + "\"" +
                "}";
        mockResponseWithCodeAndContent(200, responseContent);

        //when
        EchoResponse echoResponse = loginService.getMessageAndQuantity("test", "test");

        //then
        assertThat(echoResponse.getQuantity()).isEqualTo(expectedQuantity);
    }


    protected void mockResponseWithCodeAndContent(int httpCode, String content) throws IOException {
        Response response = createResponseWithCodeAndJson(httpCode, content);
        when(client.execute(Matchers.anyObject())).thenReturn(response);
    }

    private Response createResponseWithCodeAndJson(int responseCode, String json) {
        return new Response(responseCode, "nothing", Collections.EMPTY_LIST, new TypedByteArray("application/json", json.getBytes()));
    }

另请阅读:Square retrofit server mock for testing

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2014-05-20
    • 2016-05-01
    • 2016-10-03
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 2016-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多