How to test a REST API that uses Retrofit and Dagger2

I would like to create a unit test that checks that the API successfully retrieves the Repo List. I would like the test to actually make a network connection instead of using Mock Server. And also, it would be useful to use RoboElectric, for example, so that the test can run on the JVM.

This is to test the model from the MVP architecture.

I use a design sample from Mosby, it uses Dagger 2 and Retrofit 1.9.

public interface GithubApi
{
    @GET("/repositories")
    @Headers("Cache-Control: no-cache")
    public void getRepos(Callback<List<Repo>> callback);
}

This is the module:

@Module()
public class SampleModule
{
    @Provides @Singleton public GithubApi providesGithubApi()
    {
        OkHttpClient client = new OkHttpClient();
        client.setCache(new Cache(context.getCacheDir(), 10 * 1024 * 1024));

        RestAdapter restAdapter = new RestAdapter.Builder()
            .setClient(new OkClient(client))
            .setEndpoint("https://api.github.com")
            .build();

        return restAdapter.create(GithubApi.class);
    }
}
+4
source share
2 answers

What do you really want to check? I ask because it seems to you that it is not clear what you really want:

  • API? API.
  • , http-? , "". Retrofit . , , .
  • json? gson, Retrofit. , .

, : "" ( - MVP)? - -, ( , ):

  • POJO, gson/jackson json, API Github, . ? live API. . unit test, GithubApi , .
  • , HTTP . API Github, , API Github 404, 401 ..

, , , "", .

, Mock Server.

, , MockServer.

, . , ?

, , GithubAPI ?

, " " Robolectric, . , ! assertEquals() .. ? , , , . , , , Espresso , recylcerview . . , , " ". .

: ? ? , , . , RecyclerView , RecyclerView Android.

+18

Source: https://habr.com/ru/post/1625909/


All Articles