mardi 20 octobre 2015

Android ActivityInstrumentationTestCase2 + Retrofit + Mockito?

i am using Retrofit successfully and currently i'm facing with ActivityInstrumentationTestCase2 to test my app therefore Retrofit. With classical test cases i'm running ok, but i have no the right concepts to run a Retrofit test though i've researched through the web, but the cases i've figured out has no coincidence with my scenario. This is my method i'd like to test:

        Call<Geonames> callRetrofit = Retrofit_CiudadApi.getInstance().getMetaCiudad(editCiudad.getText().toString());
        callRetrofit.enqueue(new Callback<Geonames>() {
            @Override
            public void onResponse(Response<Geonames> response) {
                Geonames geoCiudades = response.body();
                Log.i(TAG, "url " + response.raw().request().httpUrl());
                if (geoCiudades == null) {
                    Log.e(TAG, "error obteniendo datos ciudad");
                    btn_mapa.setVisibility(View.INVISIBLE);
                } else {
                    Log.i(TAG, "success obteniendo datos ciudad");
                    if (geoCiudades.geonames != null && geoCiudades.geonames.size() > 0) {
                        btn_mapa.setVisibility(View.VISIBLE);
                        interfrag.setGeoname(geoCiudades.geonames.get(0));
                        Interface_APIGeoTemp apiCity = new API_GeoTemp();
                        apiCity.consultaTemperaturaCiudad(FrmTemperatura.this, geoCiudades.geonames.get(0));
                    }
                }
            }
            @Override
            public void onFailure(Throwable t) {
                Log.e(TAG, "throbale: " + t.getMessage());
            }
        });

Now, in my MainActivityTest which extends ActivityInstrumentationTestCase2 i added code as follows:

@Mock
private Call<Geonames> mockApiReq;
Geonames geoCiudades = null;

public void test_mockito()
{
    mockApiReq = Retrofit_CiudadApi.getInstance().getMetaCiudad("Madrid");
    mockApiReq.enqueue(new Callback<Geonames>() {
        @Override
        public void onResponse(Response<Geonames> response) {
            geoCiudades = response.body();
            assertEquals("Madrid", geoCiudades.geonames.get(0).name);
        }
        @Override
        public void onFailure(Throwable t) {
            assertNotNull(geoCiudades);
            assertNull(t.getMessage().toString());
            //Log.e(TAG, "throbale: " + t.getMessage());
        }
    });
}

I know i'm not using Mockito currently, but when i faced with its methods, i do not know how to proceed (i've read the docs). I know it would be something related with:

   Mockito.when(mockApiReq).thenReturn(???);

Anyone could help to me with this? Thanks!!

Aucun commentaire:

Enregistrer un commentaire