Через некоторое время я понял, что 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;
}
}