### Question:
ParsePushController { public Task<Void> sendInBackground(ParsePush.State state, String sessionToken) { return buildRESTSendPushCommand(state, sessionToken).executeAsync(restClient).makeVoid(); } ParsePushController(ParseHttpClient restClient); Task<Void> sendInBackground(ParsePush.State state, String sessionToken);  }### Answer:
@Test public void testSendInBackgroundSuccess() throws Exception { JSONObject mockResponse = new JSONObject(); mockResponse.put("result", "OK"); ParseHttpClient restClient = mockParseHttpClientWithResponse(mockResponse, 200, "OK"); ParsePushController controller = new ParsePushController(restClient); JSONObject data = new JSONObject(); data.put(ParsePush.KEY_DATA_MESSAGE, "hello world"); ParsePush.State state = new ParsePush.State.Builder() .data(data) .build(); Task<Void> pushTask= controller.sendInBackground(state, "sessionToken"); ParseTaskUtils.wait(pushTask); verify(restClient, times(1)).execute(any(ParseHttpRequest.class)); }
@Test public void testSendInBackgroundFailWithIOException() throws Exception { ParseRequest.setDefaultInitialRetryDelay(1L); ParseHttpClient restClient = mock(ParseHttpClient.class); when(restClient.execute(any(ParseHttpRequest.class))).thenThrow(new IOException()); ParsePushController controller = new ParsePushController(restClient); JSONObject data = new JSONObject(); data.put(ParsePush.KEY_DATA_MESSAGE, "hello world"); ParsePush.State state = new ParsePush.State.Builder() .data(data) .build(); Task<Void> pushTask= controller.sendInBackground(state, "sessionToken"); pushTask.waitForCompletion(); verify(restClient, times(5)).execute(any(ParseHttpRequest.class)); assertTrue(pushTask.isFaulted()); Exception error = pushTask.getError(); assertThat(error, instanceOf(ParseException.class)); assertEquals(ParseException.CONNECTION_FAILED, ((ParseException) error).getCode()); }
@Test public void testSendInBackgroundSuccess() throws Exception { JSONObject mockResponse = new JSONObject(); mockResponse.put("result", "OK"); ParseHttpClient restClient = mockParseHttpClientWithResponse(mockResponse, 200, "OK"); ParsePushController controller = new ParsePushController(restClient); JSONObject data = new JSONObject(); data.put(ParsePush.KEY_DATA_MESSAGE, "hello world"); ParsePush.State state = new ParsePush.State.Builder() .data(data) .build(); Task<Void> pushTask = controller.sendInBackground(state, "sessionToken"); ParseTaskUtils.wait(pushTask); verify(restClient, times(1)).execute(any(ParseHttpRequest.class)); }
@Test public void testSendInBackgroundFailWithIOException() throws Exception { ParseRequest.setDefaultInitialRetryDelay(1L); ParseHttpClient restClient = mock(ParseHttpClient.class); when(restClient.execute(any(ParseHttpRequest.class))).thenThrow(new IOException()); ParsePushController controller = new ParsePushController(restClient); JSONObject data = new JSONObject(); data.put(ParsePush.KEY_DATA_MESSAGE, "hello world"); ParsePush.State state = new ParsePush.State.Builder() .data(data) .build(); Task<Void> pushTask = controller.sendInBackground(state, "sessionToken"); pushTask.waitForCompletion(); verify(restClient, times(5)).execute(any(ParseHttpRequest.class)); assertTrue(pushTask.isFaulted()); Exception error = pushTask.getError(); assertThat(error, instanceOf(ParseException.class)); assertEquals(ParseException.CONNECTION_FAILED, ((ParseException) error).getCode()); }