I would like to create a unit test that verifies that the API successfully gets the List of Repos. I would like that the test actually makes the network connection instead of using a Mock Server. And also, would be good to use RoboElectric for example so the test can run on the JVM.
This to test the Model from the MVP architecture.
I am using a sample project from Mosby, this 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);
}
}
What do you actually want to test? I'm asking because it seems that you are not clear what you actually want:
So still the same question: what do you actually want to test on "Model" (business logic layer in MVP)? If retrofit is your whole business logic, than the following things (not already tested by external libraries as mentioned above) can be tested:
So that are the kind of unit tests you can write for a Unit testing the "model" if your model is just retrofit.
I would like that the test actually makes the network connection instead of using a Mock Server.
I hope that you now understand that there is a need of a MockServer.
Otherwise it sounds that you want to write a kind of integration test. But again, what do you actually want to test with that integration test?
To me it seems that you just want to ensure that your app is not crashing when loading data from GithubAPI and scrolling in a list of items in your UI, right?
So such "integration tests" can be written with both Robolectric and Espresso. But, that's actually not a test! do you have any assertEquals()
etc in such a test? So it's a pretty dumb test because you can't verify nothing except that your app is not crashing. If that is what you are looking for, go ahead, write an Espresso test for your activity that loads live data and scrolls your recylcerview all the way down until the end. But you actually have covered pretty nothing with such a test. It's not reliable, reproduceable and only verifies that your app is not crashing in the "happy path". But that's pretty the same as doing it manually when launching the app.
So ask yourself: What do I want to test? What actually should I test? As said, testing retrofit doesn't make sense because it's already tested by square. Testing that scrolling a RecyclerView works doesn't make sense either because the UI widget RecyclerView is already tested internally in the android framework.