使用同步调用改造2进行单元测试android

时间:2018-06-27 23:56:16

标签: android unit-testing retrofit2 mockwebserver

我正在开发一个基于干净架构的应用程序,为此我使用了Dagger 2,rxJava,retrofit 2和realm。我使用rxJava在远程层中同步调用服务,并在演示中异步调用服务,该应用程序现在正在运行,现在我想实现单元测试以报告代码覆盖率,本地层的单元论已经存在,我无法对经过改造的服务调用层进行单元测试,在这种情况下如何进行单元测试?

Project Structure

Call to service with retrofit

RestApi.java

在MockWebServer响应中,单元测试代码就可以了,我正在创建一个带有两个值的json,但是调用了对Web服务列表的调用方法list appList = cloudAppRepository.getApps();它返回一个没有记录的列表

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

1 个答案:

答案 0 :(得分: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;
}

}