### Question:
ParseCloudCodeController { public <T> Task<T> callFunctionInBackground(final String name, final Map<String, ?> params, String sessionToken) { ParseRESTCommand command = ParseRESTCloudCommand.callFunctionCommand( name, params, sessionToken); return command.executeAsync(restClient).onSuccess(new Continuation<JSONObject, T>() { @Override public T then(Task<JSONObject> task) throws Exception { @SuppressWarnings("unchecked") T result = (T) convertCloudResponse(task.getResult()); return result; } }); } ParseCloudCodeController(ParseHttpClient restClient); Task<T> callFunctionInBackground(final String name,
      final Map<String, ?> params, String sessionToken);  }### Answer:
@Test public void testCallFunctionInBackgroundSuccessWithResult() throws Exception { JSONObject json = new JSONObject(); json.put("result", "test"); String content = json.toString(); ParseHttpResponse mockResponse = new ParseHttpResponse.Builder() .setStatusCode(200) .setTotalSize((long) content.length()) .setContent(new ByteArrayInputStream(content.getBytes())) .build(); ParseHttpClient restClient = mockParseHttpClientWithReponse(mockResponse); ParseCloudCodeController controller = new ParseCloudCodeController(restClient); Task<String> cloudCodeTask = controller.callFunctionInBackground( "test", new HashMap<String, Object>(), "sessionToken"); ParseTaskUtils.wait(cloudCodeTask); verify(restClient, times(1)).execute(any(ParseHttpRequest.class)); assertEquals("test", cloudCodeTask.getResult()); }
@Test public void testCallFunctionInBackgroundSuccessWithoutResult() throws Exception { JSONObject json = new JSONObject(); String content = json.toString(); ParseHttpResponse mockResponse = new ParseHttpResponse.Builder() .setStatusCode(200) .setTotalSize((long) content.length()) .setContent(new ByteArrayInputStream(content.getBytes())) .build(); ParseHttpClient restClient = mockParseHttpClientWithReponse(mockResponse); ParseCloudCodeController controller = new ParseCloudCodeController(restClient); Task<String> cloudCodeTask = controller.callFunctionInBackground( "test", new HashMap<String, Object>(), "sessionToken"); ParseTaskUtils.wait(cloudCodeTask); verify(restClient, times(1)).execute(any(ParseHttpRequest.class)); assertNull(cloudCodeTask.getResult()); }
@Test public void testCallFunctionInBackgroundFailure() throws Exception { ParseRequest.setDefaultInitialRetryDelay(1L); ParseHttpClient restClient = mock(ParseHttpClient.class); when(restClient.execute(any(ParseHttpRequest.class))).thenThrow(new IOException()); ParseCloudCodeController controller = new ParseCloudCodeController(restClient); Task<String> cloudCodeTask = controller.callFunctionInBackground("test", new HashMap<String, Object>(), "sessionToken"); cloudCodeTask.waitForCompletion(); verify(restClient, times(5)).execute(any(ParseHttpRequest.class)); assertTrue(cloudCodeTask.isFaulted()); Exception error = cloudCodeTask.getError(); assertThat(error, instanceOf(ParseException.class)); assertEquals(ParseException.CONNECTION_FAILED, ((ParseException) error).getCode()); }