### Question:
ApiGateway { protected void dispatch(T apiResponse, ApiResponseCallbacks<T> responseCallbacks) { if (apiResponse.isSuccess()) { try { responseCallbacks.onSuccess(apiResponse); } catch (Exception e) { e.printStackTrace(); Log.e(e.getClass().getName(), "ApiGateway:dispatch:Error:\n" + e.getMessage()); responseCallbacks.onProcessFailure(apiResponse); } } else { Log.e("request failed", "ApiGateway:response:\n" + apiResponse.getHttpResponseCode()); responseCallbacks.onRequestFailure(apiResponse); } responseCallbacks.onComplete(); }  T makeSyncRequest(ApiRequest<T> apiRequest); @TargetApi(Build.VERSION_CODES.CUPCAKE) void makeRequest(ApiRequest<T> apiRequest, final ApiResponseCallbacks<T> responseCallbacks);  }### Answer:
@Test public void dispatch_shouldCallOnSuccessWhenApiResponseIsSuccess() throws Exception { JsonApiResponse jsonApiResponse = new JsonApiResponse(200); apiGateway.dispatch(jsonApiResponse, responseCallbacks); assertThat(responseCallbacks.successResponse, sameInstance(jsonApiResponse)); assertThat(responseCallbacks.failureResponse, nullValue()); assertThat(responseCallbacks.onCompleteWasCalled, equalTo(true)); }
@Test public void dispatch_shouldCallOnFailureWhenApiResponseIsFailure() throws Exception { JsonApiResponse jsonApiResponse = new JsonApiResponse(500); apiGateway.dispatch(jsonApiResponse, responseCallbacks); assertThat(responseCallbacks.failureResponse, sameInstance((ApiResponse) jsonApiResponse)); assertThat(responseCallbacks.successResponse, nullValue()); assertThat(responseCallbacks.onCompleteWasCalled, equalTo(true)); }
@Test public void dispatch_shouldCallOnFailureWhenProcessRespFails() { JsonApiResponse jsonApiResponse = new JsonApiResponse(200); TestApiResponseCallbacks callbacks = new TestApiResponseCallbacks() { @Override public void onSuccess(JsonApiResponse successResponse) throws IOException { throw new RuntimeException("Boom!"); } }; apiGateway.dispatch(jsonApiResponse, callbacks); assertThat(callbacks.onProcessFailureWasCalled, equalTo(true)); assertThat(callbacks.onCompleteWasCalled, equalTo(true)); }
@Test public void dispatch_shouldCallOnFailureWhenCallbackFails() { JsonApiResponse jsonApiResponse = new JsonApiResponse(200); TestApiResponseCallbacks callbacks = new TestApiResponseCallbacks() { @Override public void onSuccess(JsonApiResponse successResponse) throws IOException { try { callbackFailed.onSuccess(); } catch (Exception e) { onCallbackFailure(successResponse); } } }; apiGateway.dispatch(jsonApiResponse, callbacks); assertThat(callbacks.onCallBackFailureWasCalled, equalTo(true)); assertThat(callbacks.onCompleteWasCalled, equalTo(true)); }