Using Mockito with Retrofit 2.0

I am trying to create unit tests for my api calls (made through Retrofit 2.0) using Mockito .

This seemed to be the most popular blog when using Mockito with Retrofit .

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

Unfortunately, it uses earlier versions of Retrofit and depends on Callbacks and RetrofitError , which have been dropped from 2.0.

How do you do this with Retrofit 2.0 ?

PS: I use RxJava with Retrofit , so something that works with RxJava would be great. Thanks!

+7
android retrofit rx-java mockito android-testing
source share
1 answer

there is an example in the official Upgrade repository that may be useful: https://github.com/square/retrofit/tree/master/retrofit-mock

I also found: https://touk.pl/blog/2014/02/26/mock-retrofit-using-dagger-and-mockito/

Here you will find this snippet:

Unit tests

During application development, you can send requests to the server all the time (or most of the time), so you can live without a ridiculed server, this sucks, but it is possible. Unfortunately, you cannot write tests well without a layout. Below are two unit tests. In fact, they donโ€™t test anything, but in a simple way shows how to mock Retrofit with 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\"," + " \"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())); } 

See also: Square modified server for testing

Hope this helps

0
source share

All Articles