### Question:
ApiGateway { public <T extends ApiResponse> void makeRequest(ApiRequest<T> apiRequest, final ApiResponseCallbacks<T> responseCallbacks) { new RemoteCallTask(responseCallbacks).execute(apiRequest); }  void makeRequest(ApiRequest<T> apiRequest, final ApiResponseCallbacks<T> responseCallbacks);  }### Answer:
@Test public void dispatch_shouldCallOnFailureWhenXmlParseErrorOccurs() throws Exception { Robolectric.addPendingHttpResponse(200, "Invalid x-shmail"); apiGateway.makeRequest(new TestGetRequest(), responseCallbacks); assertThat(responseCallbacks.successResponse, nullValue()); assertThat(responseCallbacks.failureResponse, not(nullValue())); assertThat(responseCallbacks.onCompleteWasCalled, equalTo(true)); }
@Test public void shouldMakeRemoteGetCalls() { Robolectric.getBackgroundScheduler().pause(); TestGetRequest apiRequest = new TestGetRequest(); apiGateway.makeRequest(apiRequest, responseCallbacks); Robolectric.addPendingHttpResponse(200, GENERIC_XML); Robolectric.getBackgroundScheduler().runOneTask(); HttpRequestInfo sentHttpRequestData = Robolectric.getSentHttpRequestInfo(0); HttpRequest sentHttpRequest = sentHttpRequestData.getHttpRequest(); assertThat(sentHttpRequest.getRequestLine().getUri(), equalTo("www.example.com")); assertThat(sentHttpRequest.getRequestLine().getMethod(), equalTo(HttpGet.METHOD_NAME)); assertThat(sentHttpRequest.getHeaders("foo")[0].getValue(), equalTo("bar")); CredentialsProvider credentialsProvider = (CredentialsProvider) sentHttpRequestData.getHttpContext().getAttribute(ClientContext.CREDS_PROVIDER); assertThat(credentialsProvider.getCredentials(AuthScope.ANY).getUserPrincipal().getName(), CoreMatchers.equalTo("spongebob")); assertThat(credentialsProvider.getCredentials(AuthScope.ANY).getPassword(), CoreMatchers.equalTo("squarepants")); }
@Test public void shouldMakeRemotePostCalls() throws Exception { Robolectric.getBackgroundScheduler().pause(); TestPostRequest apiRequest = new TestPostRequest(); apiGateway.makeRequest(apiRequest, responseCallbacks); Robolectric.addPendingHttpResponse(200, GENERIC_XML); Robolectric.getBackgroundScheduler().runOneTask(); HttpRequestInfo sentHttpRequestData = Robolectric.getSentHttpRequestInfo(0); HttpRequest sentHttpRequest = sentHttpRequestData.getHttpRequest(); assertThat(sentHttpRequest.getRequestLine().getUri(), equalTo("www.example.com")); assertThat(sentHttpRequest.getRequestLine().getMethod(), equalTo(HttpPost.METHOD_NAME)); assertThat(sentHttpRequest.getHeaders("foo")[0].getValue(), equalTo("bar")); InputStream contentStream = ((HttpPost) sentHttpRequest).getEntity().getContent(); assertThat(Strings.fromStream(contentStream), CoreMatchers.equalTo("post body content")); CredentialsProvider credentialsProvider = (CredentialsProvider) sentHttpRequestData.getHttpContext().getAttribute(ClientContext.CREDS_PROVIDER); assertThat(credentialsProvider.getCredentials(AuthScope.ANY).getUserPrincipal().getName(), CoreMatchers.equalTo("spongebob")); assertThat(credentialsProvider.getCredentials(AuthScope.ANY).getPassword(), CoreMatchers.equalTo("squarepants")); }
@Test public void shouldDispatchUponReceivingResponse() throws Exception { Robolectric.getBackgroundScheduler().pause(); Robolectric.getUiThreadScheduler().pause(); Robolectric.addPendingHttpResponse(200, GENERIC_XML); apiGateway.makeRequest(new TestGetRequest(), responseCallbacks); Robolectric.getBackgroundScheduler().runOneTask(); assertThat(responseCallbacks.successResponse, nullValue()); Robolectric.getUiThreadScheduler().runOneTask(); assertThat(asString(responseCallbacks.successResponse.getResponseDocument()), equalTo(GENERIC_XML)); }