2015-11-19 12 views
7

Sto provando a creare test unitari per le mie chiamate api (effettuate tramite Retrofit 2.0) utilizzando Mockito.Utilizzo di Mockito con Retrofit 2.0

Questo sembrava essere il blog più popolare sull'utilizzo di Mockito con Retrofit.

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

Purtroppo, utilizza versioni precedenti di Retrofit, e dipende dalla Callbacks e RetrofitError, che sono interrotto da 2,0.

Come si fa a questo con Retrofit 2.0?

P.S .: Sto usando RxJava insieme a retrofit, quindi qualcosa che funziona con RxJava sarebbe fantastico. Grazie!

+0

Hai guardato le classi MockWebServer e MockResponse di OkHttp? Possono fare tutto questo senza la dipendenza da Mockito ... –

risposta

0

sul repository ufficiale di Retrofit c'è un esempio che può essere utile: https://github.com/square/retrofit/tree/master/retrofit-mock

ho trovato anche: https://touk.pl/blog/2014/02/26/mock-retrofit-using-dagger-and-mockito/

Qui si dovrebbe trovare questo frammento:

Unità Test

Durante lo sviluppo di app, è possibile inviare richieste al server in qualsiasi momento (o la maggior parte di ime) quindi è possibile vivere senza server deriso, è fa schifo ma è possibile. Purtroppo non sei in grado di scrivere buoni test senza la simulazione. Di seguito ci sono due test unitari. In realtà sono non testare nulla ma in modo semplice mostra come simulare il servizio Retrofit utilizzando Mockito e 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())); 
    } 

Leggi anche: Square retrofit server mock for testing

speranza è aiutare

Problemi correlati