У меня есть два вызова API: один для отправки, а другой - для получения. Мне нужно провести модульное тестирование для того же. Пожалуйста, помогите мне завершить. Я использую Rx java, также необходимо дополнить его тем же.
class APIClient {
private static Retrofit retrofit = null;
static Retrofit getClient() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
//OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(new Interceptor() {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request original = chain.request();
Request request = original.newBuilder()
.header("User-Agent", "Your-App-Name")
.header("Accept", "application/vnd.yourapi.v1.full+json")
.method(original.method(), original.body())
.build();
return chain.proceed(request);
}
});
OkHttpClient client = httpClient.build();
retrofit = new Retrofit.Builder()
.baseUrl("https://jsonplaceholder.typicode.com")
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
return retrofit;
}
}
Класс обслуживания
public class UserService {
APIInterface apiInterface;
public UserService(APIInterface service) {
this.apiInterface = service;
}
}
Интерфейс
public interface APIInterface {
@GET("/posts")
Observable<List<GetResponse>> doGetListResources();
@POST("/posts")
Observable<GetResponse> getPostResponse(@Body PostBody postBody);
}
То, что я пробовал до сих пор при тестировании
public class ExampleUnitTest {
@Rule
public MockitoRule rule = MockitoJUnit.rule();
@Mock
APIInterface apiInterface;
@InjectMocks
UserService userService;
@Mock
Observable<GetResponse> mockCall;
@Test
public void addition_isCorrect() {
assertEquals(4, 2 + 2);
}
@Test
public void getAPITest() {
userService.apiInterface.doGetListResources();
when(userService.apiInterface.doGetListResources()).
thenReturn(Observable.<List<GetResponse>>just(Collections.singletonList(new GetResponse())));
}
public Observable<GetResponse> getObservable() {
return Observable.just(new GetResponse(1, 2, "ti", "mat"));
}
@Test
public void postAPiTest() {
Observable<GetResponse> obs = getObservable();
when(userService.apiInterface.getPostResponse(new PostBody("", "", 2))).
thenReturn(Observable.just(new GetResponse(1, 2, "ti", "mat")));
Assert.assertEquals(mockCall, userService.apiInterface.getPostResponse(new PostBody("", "", 2)));
}
}
Я проверял postAPITest () и он всегда терпит неудачу. Пожалуйста, дайте мне знать, как это исправить.
Заранее спасибо