### Question:
UploaderTask extends BackgroundTask { @Override public int start(TaskStatusMonitor progress) { progress.startProgress(this.getDescription(), 3); return run(progress); } UploaderTask(ProjectUploader uploader, String path, ProjectDAO projectDAO, IdeUIInvoker invoker); @Override int start(TaskStatusMonitor progress); SubmissionResult getResult(); Project getProject();  }### Answer:
@Test public void retunsFailureAndCallsMethodsCorrectlyWhenCancelRequestedAfterZipping() throws IOException { when(progress.isCancelRequested()).thenReturn(false); Mockito.doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) { when(progress.isCancelRequested()).thenReturn(true); return null; } }).when(uploader).zipProjects(); assertEquals(BackgroundTask.RETURN_INTERRUPTED, task.start(progress)); verify(progress, times(0)).incrementProgress(1); verify(uploader, times(1)).zipProjects(); verify(uploader, times(0)).handleSubmissionResponse(); verify(uploader, times(0)).handleSubmissionResult(Mockito.any(StopStatus.class)); }
@Test public void progressBarIsSetOnStart() { task.start(progress); verify(progress, times(1)).startProgress(anyString(), anyInt()); }
@Test public void projectIsSetToUploader() { task.start(progress); verify(uploader, times(1)).setProject(project); }
@Test public void retunsFailureAndCallsMethodsCorrectlyWhenCancelRequestedAfterHandlingResponse() throws IOException { when(progress.isCancelRequested()).thenReturn(false); Mockito.doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) { when(progress.isCancelRequested()).thenReturn(true); return null; } }).when(uploader).handleSubmissionResponse(); assertEquals(BackgroundTask.RETURN_INTERRUPTED, task.start(progress)); verify(progress, times(1)).incrementProgress(1); verify(uploader, times(1)).zipProjects(); verify(uploader, times(1)).handleSubmissionResponse(); verify(uploader, times(0)).handleSubmissionResult(Mockito.any(StopStatus.class)); }
@Test public void retunsFailureAndCallsMethodsCorrectlyWhenCancelRequestedDuringWaitingResult() throws IOException { when(progress.isCancelRequested()).thenReturn(false); Mockito.doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) { when(progress.isCancelRequested()).thenReturn(true); return null; } }).when(uploader).handleSubmissionResult(Mockito.any(StopStatus.class)); assertEquals(BackgroundTask.RETURN_FAILURE, task.start(progress)); verify(progress, times(2)).incrementProgress(1); verify(uploader, times(1)).zipProjects(); verify(uploader, times(1)).handleSubmissionResponse(); verify(uploader, times(1)).handleSubmissionResult(Mockito.any(StopStatus.class)); }
@Test public void projectUploaderCallsErrorHandlerAndReturnsFalseOnException() throws IOException { Mockito.doThrow(new RuntimeException("Error message here")).when(uploader).zipProjects(); assertEquals(BackgroundTask.RETURN_FAILURE, task.start(progress)); verify(invoker, times(1)).raiseVisibleException( "An error occurred while uploading exercises:\nError message here"); }