1

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?

Project Structure

Call to service with retrofit

RestApi.java

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;
}

}

Test Result

4

1 回答 1

0

一段时间后,我意识到 MockWebServe 正在调用另一个 url,这里我离开你如何解决这个问题。

public class CloudAppRepositoryUnitTest {
private AppEntityRemoteMapper appEntityRemoteMapper;
private CloudAppRepository cloudAppRepository;
private MockWebServer mockWebServer;

@Before
public void setUp() throws Exception {
    mockWebServer = new MockWebServer();
    ApplicationEntity applicationEntityResponse = createApplicationEntity();
    String response = new Gson().toJson(applicationEntityResponse);
    MockResponse mockResponse = new MockResponse().setResponseCode(200)
            .setBody(response);
    mockWebServer.enqueue(mockResponse);
    mockWebServer.start();
    OkHttpClient client = new OkHttpClient.Builder().build();
    Retrofit retrofit = new Retrofit.Builder()
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl(mockWebServer.url("/"))
            .client(client)
            .build();
    RestApi restApi = retrofit.create(RestApi.class);
    appEntityRemoteMapper = new AppEntityRemoteMapper();
    cloudAppRepository = new CloudAppRepository(restApi, appEntityRemoteMapper);
}

@After
public void tearDown() throws Exception {
    this.mockWebServer.shutdown();
}

@Test
public void testGetApps() throws Exception {
    List<App> appList = cloudAppRepository.getApps();
    assertThat(appList, CoreMatchers.notNullValue());
    assertThat(appList.size(), is(2));
    assertThat(appList.get(0), is(instanceOf(App.class)));
    assertThat(appList.get(0).getId(), is(1));
    assertThat(appList.get(0).getName(), is("Mi Claro App"));
    assertThat(appList.get(0).getTopic(), is("miclaroapp"));
    assertThat(appList.get(0).getDescription(), is("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua."));
}

@Test
public void testSubscriberApp() throws Exception {
    /*List<App> appList = createAppList();
    boolean response = cloudAppRepository.subscriberApp(appList);
    assertThat(response, is(true));*/
}

private List<App> createAppList() {
    List<App> appList = new ArrayList<>();
    appList.add(new App(1, "Mi Claro App", "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua.",
            true, "miclaroapp"));
    appList.add(new App(2, "Mi Claro Web", "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua.",
            false, "miclaroweb"));
    return appList;
}

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 Claro Web", "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua.",
            true, "miclaroweb"));
    return appListResponse;
}

private Status createStatus() {
    Status status = new Status();
    status.setCode(1);
    status.setMessage("correct");
    return status;
}

}

于 2018-07-03T17:39:00.173 回答