android – 使用Mockito和Retrofit 2.0

android – 使用Mockito和Retrofit 2.0,第1张

概述我正在尝试使用Mockito为我的api调用(通过Retrofit 2.0制作)创建单元测试. 这似乎是使用Mockito和Retrofit最受欢迎的博客. http://mdswanson.com/blog/2013/12/16/reliable-android-http-testing-with-retrofit-and-mockito.html 不幸的是,它使用了早期版本的Retrofit, 我正在尝试使用Mockito为我的API调用(通过Retrofit 2.0制作)创建单元测试.

这似乎是使用Mockito和Retrofit最受欢迎的博客.

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

不幸的是,它使用了早期版本的Retrofit,并且依赖于Callbacks和RetrofitError,它们已从2.0中断.

你如何使用Retrofit 2.0做到这一点?

P.S.:我正在使用RxJava和改造,所以适用于RxJava的东西会很棒.谢谢!

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

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

在这里你会发现这个片段:

Unit Tests

During develop of app,you can send requests the server all time(or
most of time) so it is possible to live without mocked server,it
sucks but is possible. Unfortunately you are not able to write good
tests without the mock. Below there are two unit tests. Actually they
do not test anything but in simple way shows how to mock Retrofit
service using Mockito and Dagger.

@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\","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

希望它有所帮助

总结

以上是内存溢出为你收集整理的android – 使用Mockito和Retrofit 2.0全部内容,希望文章能够帮你解决android – 使用Mockito和Retrofit 2.0所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1135633.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存