### Question:
TaskRunner extends Thread { protected void heartbeat() { long now = System.currentTimeMillis(); if (lastHb < (now - hbPeriod)) { lastHb = now; try { taskPersister.heartbeat(nodeId, new HashSet<>(runningTasks.keySet()), hbPeriod * 5); } 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", "rawtypes" }) @Test public void heartbeat() { TaskRunner tested = getTested(); { long lhb = System.currentTimeMillis() - tested.hbPeriod + 200; tested.lastHb = lhb; tested.heartbeat(); Assert.assertEquals(lhb, tested.lastHb); Mockito.verifyZeroInteractions(tested.taskPersister); } { Mockito.reset(tested.taskPersister); long lhb = System.currentTimeMillis() - tested.hbPeriod - 200; tested.lastHb = lhb; Mockito.doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { Set<String> s = (Set<String>) invocation.getArguments()[1]; Assert.assertNotNull(s); Assert.assertTrue(s.isEmpty()); return null; } }).when(tested.taskPersister).heartbeat(Mockito.eq(TESTNODEID), Mockito.anySet(), Mockito.anyLong()); tested.heartbeat(); TestUtils.assertCurrentDate(tested.lastHb); Mockito.verify(tested.taskPersister).heartbeat(Mockito.eq(TESTNODEID), Mockito.anySet(), Mockito.eq(tested.hbPeriod * 5)); Mockito.verifyNoMoreInteractions(tested.taskPersister); } { Mockito.reset(tested.taskPersister); tested.runningTasks.put("tid1", Mockito.mock(Task.class)); tested.runningTasks.put("tid2", Mockito.mock(Task.class)); long lhb = System.currentTimeMillis() - tested.hbPeriod - 200; tested.lastHb = lhb; Mockito.doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { Set<String> s = (Set<String>) invocation.getArguments()[1]; Assert.assertNotNull(s); Assert.assertEquals(2, s.size()); Assert.assertTrue(s.contains("tid1")); Assert.assertTrue(s.contains("tid2")); return null; } }).when(tested.taskPersister).heartbeat(Mockito.eq(TESTNODEID), Mockito.anySet(), Mockito.anyLong()); tested.heartbeat(); TestUtils.assertCurrentDate(tested.lastHb); Mockito.verify(tested.taskPersister).heartbeat(Mockito.eq(TESTNODEID), Mockito.anySet(), Mockito.eq(tested.hbPeriod * 5)); Mockito.verifyNoMoreInteractions(tested.taskPersister); } }