CreateTaskInteractor implements Interactor { public void createTask(String name, String description, Boolean urgent, Callback<Task> callback, ErrorCallback errorCallback) { this.name = name; this.description = description; this.urgent = urgent; this.callback = callback; this.errorCallback = errorCallback; interactorHandler.execute(this); } @Inject  CreateTaskInteractor(InteractorHandler interactorHandler, TaskRepository taskRepository, TaskListRepository taskListRepository); void createTask(String name, String description, Boolean urgent, Callback<Task> callback, ErrorCallback errorCallback); @Override void run();  }
@Test public void shouldGetTaskListsBeforeCreating() throws Exception { when(taskListRepository.getAll()).thenReturn(taskLists()); InOrder order = inOrder(taskRepository, taskListRepository); interactor.createTask(NAME_STUB, DESCRIPTION_STUB, URGENT_STUB, callback, errorCallback); order.verify(taskListRepository).getAll(); order.verify(taskRepository).put(any(Task.class)); }
@Test public void shouldCreateTaskWithProjectIdAndListIdFromFirstTaskList() throws Exception { when(taskListRepository.getAll()).thenReturn(taskLists()); interactor.createTask(NAME_STUB, DESCRIPTION_STUB, URGENT_STUB, callback, errorCallback); ArgumentCaptor<Task> taskArgumentCaptor = ArgumentCaptor.forClass(Task.class); verify(taskRepository, times(1)).put(taskArgumentCaptor.capture()); Task createdTask = taskArgumentCaptor.getValue(); assertThat(createdTask.getProjectId()).isEqualTo(PROJECT_ID); assertThat(createdTask.getTaskListId()).isEqualTo(TASK_LIST_1); }
@Test public void shouldNotifyErrorToCallbackWhenTaskRepositoryFails() throws Exception { when(taskRepository.put(any(Task.class))).thenThrow(new RedboothException("Error")); interactor.createTask(NAME_STUB, DESCRIPTION_STUB, URGENT_STUB, callback, errorCallback); verify(errorCallback, times(1)).onError(any(RedboothException.class)); }
@Test public void shouldNotNotifyTaskCreatedWhenTaskRepositoryFails() throws Exception { when(taskRepository.put(any(Task.class))).thenThrow(new RedboothException("Error")); interactor.createTask(NAME_STUB, DESCRIPTION_STUB, URGENT_STUB, callback, errorCallback); verify(callback, never()).onLoaded(any(Task.class)); }
@Test public void shouldNotifyErrorToCallbackWhenTaskListEmpty() throws Exception { when(taskListRepository.getAll()).thenReturn(new ArrayList<TaskList>()); interactor.createTask(NAME_STUB, DESCRIPTION_STUB, URGENT_STUB, callback, errorCallback); verify(errorCallback, times(1)).onError(any(RedboothException.class)); verify(callback, never()).onLoaded(any(Task.class)); }