I am developing an application based on clean architecture, for which I use Dagger 2, rxJava, retrofit 2 and realm. I call the services synchronically in a remote layer, and asynchronously in the presentation by using rxJava, the application is now working, now I want to implement unit tests to have a report of code coverage, the tests Unitarians for the local layer are already there, I am having problems to do unit tests to the layer where I call the services with retrofit, how could the unit tests for this case?
The unit test code, this will be fine, in the MockWebServer response I am creating a json with two values but calling the methodo of the call to the webservice List appList = cloudAppRepository.getApps (); it returns a list with no record
public class CloudAppRepositoryUnitTest {
@Mock
private RestApi restApi;
@Mock
private AppEntityRemoteMapper appEntityRemoteMapper;
private CloudAppRepository cloudAppRepository;
private MockWebServer mockWebServer;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
mockWebServer = new MockWebServer();
mockWebServer.start();
Retrofit retrofit = new Retrofit.Builder()
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(mockWebServer.url("/"))
.build();
restApi = retrofit.create(RestApi.class);
cloudAppRepository = new CloudAppRepository(restApi, appEntityRemoteMapper);
}
@After
public void tearDown() throws Exception {
this.mockWebServer.shutdown();
}
@Test
public void testGetApps() throws Exception {
ApplicationEntity applicationEntityResponse = createApplicationEntity();
mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(new Gson().toJson(applicationEntityResponse)));
List<App> appList = cloudAppRepository.getApps();
assertThat(appList, CoreMatchers.notNullValue());
assertThat(appList.size(), is(2));
assertThat(appList.size(), is(0));
}
private ApplicationEntity createApplicationEntity() {
ApplicationEntity applicationEntity = new ApplicationEntity();
applicationEntity.setStatus(createStatus());
applicationEntity.setAppEntities(createListAppEntity());
return applicationEntity;
}
private List<AppEntity> createListAppEntity() {
List<AppEntity> appListResponse = new ArrayList<>();
appListResponse.add(new AppEntity(1, "Mi Claro App", "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua.",
true, "miclaroapp"));
appListResponse.add(new AppEntity(2, "Mi app", "description",
true, "topic1"));
return appListResponse;
}
private Status createStatus() {
Status status = new Status();
status.setCode(1);
status.setMessage("correct");
return status;
}
}