### Question:
ApiGateway { protected void dispatch(ApiResponse apiResponse, ApiResponseCallbacks responseCallbacks) { if (apiResponse.isSuccess()) { try { responseCallbacks.onSuccess(apiResponse); } catch (Exception e) { Log.e(ApiGateway.class.getName(), "Error proccessing response", e); responseCallbacks.onFailure(apiResponse); } } else { responseCallbacks.onFailure(apiResponse); } responseCallbacks.onComplete(); }  void makeRequest(ApiRequest<T> apiRequest, final ApiResponseCallbacks<T> responseCallbacks);  }### Answer:
@Test public void dispatch_shouldCallOnFailureWhenApiResponseIsFailure() throws Exception { XmlApiResponse apiResponse = new XmlApiResponse(500); apiGateway.dispatch(apiResponse, responseCallbacks); assertThat(responseCallbacks.failureResponse, sameInstance((ApiResponse) apiResponse)); assertThat(responseCallbacks.successResponse, nullValue()); assertThat(responseCallbacks.onCompleteWasCalled, equalTo(true)); }
@Test public void dispatch_shouldCallOnFailureWhenOnSuccessFails() throws Exception { XmlApiResponse apiResponse = new XmlApiResponse(200); TestApiResponseCallbacks callbacks = new TestApiResponseCallbacks() { @Override public void onSuccess(XmlApiResponse successResponse) { throw new RuntimeException("boom!"); } }; apiGateway.dispatch(apiResponse, callbacks); assertThat(callbacks.failureResponse, sameInstance((ApiResponse) apiResponse)); assertThat(callbacks.onCompleteWasCalled, equalTo(true)); }
@Test public void dispatch_shouldCallOntoTheSuccessWhenApiResponseIsSuccess() throws Exception { XmlApiResponse apiResponse = new XmlApiResponse(200); apiGateway.dispatch(apiResponse, responseCallbacks); assertThat(responseCallbacks.successResponse, sameInstance(apiResponse)); assertThat(responseCallbacks.failureResponse, nullValue()); assertThat(responseCallbacks.onCompleteWasCalled, equalTo(true)); }