### Question:
CompleteTaskAPIController { @RequestMapping(value = "/completeTask/{taskId}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public AcmTask completeTask( @PathVariable("taskId") Long taskId, Authentication authentication, HttpSession httpSession, HttpServletResponse response) throws AcmUserActionFailedException { if (log.isInfoEnabled()) { log.info("Completing task '" + taskId + "'"); } try { AcmTask completed = getTaskDao().completeTask(authentication, taskId); completed.setStatus("CLOSED"); publishTaskCompletedEvent(authentication, httpSession, completed, true); return completed; } catch (AcmTaskException e) { AcmTask fakeTask = new AcmTask(); fakeTask.setTaskId(taskId); publishTaskCompletedEvent(authentication, httpSession, fakeTask, false); throw new AcmUserActionFailedException("complete", "task", taskId, e.getMessage(), e); } }  @RequestMapping(value = "/completeTask/{taskId}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody AcmTask completeTask(
            @PathVariable("taskId") Long taskId,
            Authentication authentication,
            HttpSession httpSession,
            HttpServletResponse response); TaskDao getTaskDao(); void setTaskDao(TaskDao taskDao); TaskEventPublisher getTaskEventPublisher(); void setTaskEventPublisher(TaskEventPublisher taskEventPublisher);  }### Answer:
@Test public void completeTask() throws Exception { Long taskId = 500L; String ipAddress = "ipAddress"; AcmTask found = new AcmTask(); found.setTaskId(taskId); Capture<AcmApplicationTaskEvent> capturedEvent = new Capture<>(); mockHttpSession.setAttribute("acm_ip_address", ipAddress); expect(mockTaskDao.completeTask(eq(mockAuthentication), eq(taskId))).andReturn(found); mockTaskEventPublisher.publishTaskEvent(capture(capturedEvent)); expect(mockAuthentication.getName()).andReturn("user").atLeastOnce(); replayAll(); MvcResult result = mockMvc.perform( post("/api/v1/plugin/task/completeTask/{taskId}", taskId) .accept(MediaType.parseMediaType("application/json;charset=UTF-8")) .session(mockHttpSession) .principal(mockAuthentication)) .andReturn(); verifyAll(); assertEquals(HttpStatus.OK.value(), result.getResponse().getStatus()); assertTrue(result.getResponse().getContentType().startsWith(MediaType.APPLICATION_JSON_VALUE)); String returned = result.getResponse().getContentAsString(); log.info("results: " + returned); ObjectMapper objectMapper = new ObjectMapper(); AcmTask completedTask = objectMapper.readValue(returned, AcmTask.class); assertNotNull(completedTask); assertEquals(completedTask.getTaskId(), taskId); AcmApplicationTaskEvent event = capturedEvent.getValue(); assertEquals(taskId, event.getObjectId()); assertEquals("TASK", event.getObjectType()); assertTrue(event.isSucceeded()); }
@Test public void completeTask_exception() throws Exception { Long taskId = 500L; String ipAddress = "ipAddress"; AcmTask found = new AcmTask(); found.setTaskId(taskId); Capture<AcmApplicationTaskEvent> capturedEvent = new Capture<>(); mockHttpSession.setAttribute("acm_ip_address", ipAddress); expect(mockTaskDao.completeTask(eq(mockAuthentication), eq(taskId))).andThrow(new AcmTaskException("testException")); mockTaskEventPublisher.publishTaskEvent(capture(capturedEvent)); expect(mockAuthentication.getName()).andReturn("user").atLeastOnce(); replayAll(); mockMvc.perform( post("/api/v1/plugin/task/completeTask/{taskId}", taskId) .accept(MediaType.parseMediaType("application/json;charset=UTF-8")) .session(mockHttpSession) .principal(mockAuthentication)) .andExpect(status().isBadRequest()) .andExpect(content().contentType(MediaType.TEXT_PLAIN)); verifyAll(); AcmApplicationTaskEvent event = capturedEvent.getValue(); assertEquals(taskId, event.getObjectId()); assertEquals("TASK", event.getObjectType()); assertFalse(event.isSucceeded()); }