### Question:
TaskPersisterJpa implements TaskPersister { @Override public void writeTaskLog(String id, String message) { if (message == null || message.trim().isEmpty()) return; TaskStatusInfo tis = em.find(TaskStatusInfo.class, id, LockModeType.PESSIMISTIC_WRITE); if (tis != null) { tis.appendProcessingLog(message); } } TaskPersisterJpa(); @Override String createTask(String taskType, Map<String, Object> taskConfig); @Override List<TaskStatusInfo> listTasks(String taskTypeFilter, List<TaskStatus> taskStatusFilter, int from, int size); @Override TaskStatusInfo getTaskStatusInfo(String id); @Override boolean markTaskToBeCancelled(String id); @Override boolean changeTaskStatus(String id, TaskStatus taskStatusNew, String message); @Override void writeTaskLog(String id, String message); @Override TaskStatusInfo getTaskToRun(String nodeId); @Override void heartbeat(String nodeId, Set<String> runningTasksId, long failoverTimeout); static final long FAILOVER_DELAY_10; static final long FAILOVER_DELAY_30; static final long FAILOVER_DELAY_100; }### Answer:
@Test public void writeTaskLog() { TaskPersisterJpa tested = getTested(); tested.writeTaskLog("myid", null); Mockito.verifyZeroInteractions(tested.em); tested.writeTaskLog("myid", " "); Mockito.verifyZeroInteractions(tested.em); Mockito.reset(tested.em); Mockito.when(tested.em.find(TaskStatusInfo.class, "myid", LockModeType.PESSIMISTIC_WRITE)).thenReturn(null); tested.writeTaskLog("myid", "testmessage"); Mockito.verify(tested.em).find(TaskStatusInfo.class, "myid", LockModeType.PESSIMISTIC_WRITE); { Mockito.reset(tested.em); final TaskStatusInfo ret = createTaskStatusInfoWithStatus(TaskStatus.FAILOVER); Mockito.when(tested.em.find(TaskStatusInfo.class, "myid", LockModeType.PESSIMISTIC_WRITE)).thenReturn(ret); tested.writeTaskLog("myid", "testmessage"); Mockito.verify(tested.em).find(TaskStatusInfo.class, "myid", LockModeType.PESSIMISTIC_WRITE); Assert.assertEquals("testmessage", ret.getProcessingLog()); } }