### Question:
PastebinTask extends BackgroundTask { @Override public int start(TaskStatusMonitor progress) { progress.startProgress(this.getDescription(), 2); try { uploader.setProject(projectDAO.getProjectByFile(path)); uploader.setAsPaste(pasteMessage); uploader.zipProjects(); if (shouldStop(progress)) { return BackgroundTask.RETURN_INTERRUPTED; } progress.incrementProgress(1); uploader.handleSubmissionResponse(); if (shouldStop(progress)) { return BackgroundTask.RETURN_INTERRUPTED; } progress.incrementProgress(1); } catch (Exception ex) { invoker.raiseVisibleException("An error occurred while uploading exercise to pastebin:\n" + ex.getMessage()); return BackgroundTask.RETURN_FAILURE; } return BackgroundTask.RETURN_SUCCESS; } PastebinTask(ProjectUploader uploader, String path, String pasteMessage, ProjectDAO projectDAO,
            IdeUIInvoker invoker); @Override int start(TaskStatusMonitor progress); String getPasteUrl();  }### Answer:
@Test public void taskReturnsSuccessAfterProperInitializationOfAllComponents() throws IOException { assertEquals(BackgroundTask.RETURN_SUCCESS, task.start(progress)); verify(progress, times(2)).incrementProgress(1); verify(uploader, times(1)).setAsPaste("pastemessage"); verify(uploader, times(1)).zipProjects(); verify(uploader, times(1)).handleSubmissionResponse(); }
@Test public void taskReturnsInterruptedIfCancelIsRequestedAtFirstCheckpoint() throws IOException { when(progress.isCancelRequested()).thenReturn(true); assertEquals(BackgroundTask.RETURN_INTERRUPTED, task.start(progress)); verify(progress, times(0)).incrementProgress(1); }
@Test public void projectIsSetToUploader() { Project project = mock(Project.class); when(dao.getProjectByFile(path)).thenReturn(project); task.start(progress); verify(uploader, times(1)).setProject(project); }
@Test public void taskReturnsInterruptedIfCancelIsRequestedAtSecondCheckpoint() throws IOException { 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); }
@Test public void taskReturnsFailureIfAnExceptionIsThrown() throws IOException { doThrow(new IOException()).when(uploader).zipProjects(); assertEquals(BackgroundTask.RETURN_FAILURE, task.start(progress)); }
@Test public void invokerIsCalledIfAnExceptionIsThrown() throws IOException { doThrow(new IOException("Foobar")).when(uploader).zipProjects(); task.start(progress); verify(invoker, times(1)).raiseVisibleException( "An error occurred while uploading exercise to pastebin:\nFoobar"); }