### Question:
TaskRunner extends Thread { protected void startTasks() { try { while (runningTasks.size() < maxRunningTasks) { TaskStatusInfo tsi = taskPersister.getTaskToRun(nodeId); if (tsi == null) return; Task t = taskFactory.createTask(tsi.taskType, tsi.getTaskConfig()); t.setExecutionContext(tsi.id, taskExecutionContextInstance); runningTasks.put(tsi.id, t); t.start(); } } catch (Exception e) { log.fine(e.getMessage()); } } TaskRunner(String nodeId, TaskFactory taskFactory, TaskPersister taskPersister); protected  TaskRunner(); @Override void run(); void cancelTask(String taskId); void notifyNewTaskAvailableForRun();  }### Answer:
@SuppressWarnings("unchecked") @Test public void startTasks() throws UnsupportedTaskException, TaskConfigurationException { TaskRunner tested = getTested(); Mockito.when(tested.taskPersister.getTaskToRun(TESTNODEID)).thenReturn(null); tested.startTasks(); Mockito.verify(tested.taskPersister).getTaskToRun(TESTNODEID); Mockito.verifyNoMoreInteractions(tested.taskPersister); Mockito.verifyNoMoreInteractions(tested.taskFactory); Mockito.reset(tested.taskFactory, tested.taskPersister); Mockito.when(tested.taskPersister.getTaskToRun(TESTNODEID)).thenAnswer(new Answer<TaskStatusInfo>() { @Override public TaskStatusInfo answer(InvocationOnMock invocation) throws Throwable { return createTaskStatusInfoStartTest(); } }); Mockito.when(tested.taskFactory.createTask(Mockito.eq(TASK_TYPE_TEST), Mockito.anyMap())).thenAnswer( new Answer<Task>() { @Override public Task answer(InvocationOnMock invocation) throws Throwable { return createTaskMock(); } }); tested.startTasks(); Assert.assertEquals(2, tested.runningTasks.size()); for (Task task : tested.runningTasks.values()) { Assert.assertNotNull(task.context); Assert.assertNotNull(task.taskId); Assert.assertTrue(task.getState() != State.NEW); } Mockito.verify(tested.taskPersister, Mockito.times(2)).getTaskToRun(TESTNODEID); Mockito.verify(tested.taskFactory, Mockito.times(2)).createTask(Mockito.eq(TASK_TYPE_TEST), Mockito.anyMap()); Mockito.reset(tested.taskFactory, tested.taskPersister); tested.startTasks(); Assert.assertEquals(2, tested.runningTasks.size()); Mockito.verifyZeroInteractions(tested.taskPersister); Mockito.verifyZeroInteractions(tested.taskFactory); Mockito.reset(tested.taskFactory, tested.taskPersister); Mockito.when(tested.taskPersister.getTaskToRun(TESTNODEID)).thenThrow(new RuntimeException("test exception")); tested.startTasks(); Mockito.verifyZeroInteractions(tested.taskFactory); }