Source:
public void testOnStartCommandRebootImmediateAlarm() { PeriodicReplicationService service = new TestReplicationService(mMockContext); Intent intent = new Intent(mMockContext, TestReplicationService.class); intent.putExtra(ReplicationService.EXTRA_COMMAND, PeriodicReplicationService.COMMAND_DEVICE_REBOOTED); service.onCreate(); final CountDownLatch latch = new CountDownLatch(1); long timeReturned = System.currentTimeMillis() - ((1000 * service.getUnboundIntervalInSeconds()) * 10); when(mMockPreferences.getLong(PREFERENCE_CLASS_NAME + ".lastAlarmClock", 0)).thenReturn(timeReturned); service.setOperationStartedListener(new PeriodicReplicationService .OperationStartedListener() { @Override public void operationStarted(int operationId) { assertEquals("Unexpected command received", PeriodicReplicationService.COMMAND_DEVICE_REBOOTED, operationId); latch.countDown(); } }); ArgumentCaptor<String> captorPrefKeys = ArgumentCaptor.forClass(String.class); ArgumentCaptor<Long> captorPrefValues = ArgumentCaptor.forClass(Long.class); service.onStartCommand(intent, 0, 0); service.setReplicators(mMockReplicators); try { assertTrue("The countdown should reach zero", latch.await(DEFAULT_WAIT_SECONDS, TimeUnit.SECONDS)); verify(mMockPreferencesEditor, times(2)).putLong(captorPrefKeys.capture(), captorPrefValues.capture()); List<String> prefsKeys = captorPrefKeys.getAllValues(); List<Long> prefsValues = captorPrefValues.getAllValues(); long expectedElapsedTime = SystemClock.elapsedRealtime() - (1000 * service.getUnboundIntervalInSeconds()); long expectedRealTime = System.currentTimeMillis() - (1000 * service.getUnboundIntervalInSeconds()); checkElapsedPreferenceName(prefsKeys.get(0)); checkElapsedTime(expectedElapsedTime, prefsValues.get(0)); checkClockPreferenceName(prefsKeys.get(1)); checkClockTime(expectedRealTime, prefsValues.get(1)); } catch (InterruptedException e) { e.printStackTrace(); } }

Target:
public void testOnStartCommandRebootImmediateAlarm() { PeriodicReplicationService service = new TestReplicationService(mMockContext); Intent intent = new Intent(mMockContext, TestReplicationService.class); intent.putExtra(ReplicationService.EXTRA_COMMAND, PeriodicReplicationService.COMMAND_DEVICE_REBOOTED); service.onCreate(); final CountDownLatch latch = new CountDownLatch(1); long moreThan1ReplicationPeriodInPast = System.currentTimeMillis() - intervalMillis(ARBITRARY_NUMBER_OF_INTERVALS, service); when(mMockPreferences.getLong(PREFERENCE_CLASS_NAME + ".lastAlarmClock", 0)).thenReturn(moreThan1ReplicationPeriodInPast); service.setOperationStartedListener(new PeriodicReplicationService .OperationStartedListener() { @Override public void operationStarted(int operationId) { assertEquals("Unexpected command received", PeriodicReplicationService.COMMAND_DEVICE_REBOOTED, operationId); latch.countDown(); } }); ArgumentCaptor<String> captorPrefKeys = ArgumentCaptor.forClass(String.class); ArgumentCaptor<Long> captorPrefValues = ArgumentCaptor.forClass(Long.class); service.onStartCommand(intent, 0, 0); service.setReplicators(mMockReplicators); try { assertTrue("The countdown should reach zero", latch.await(DEFAULT_WAIT_SECONDS, TimeUnit.SECONDS)); verify(mMockPreferencesEditor, times(2)).putLong(captorPrefKeys.capture(), captorPrefValues.capture()); List<String> prefsKeys = captorPrefKeys.getAllValues(); List<Long> prefsValues = captorPrefValues.getAllValues(); long expectedElapsedTime = SystemClock.elapsedRealtime() - intervalMillis(1, service); long expectedRealTime = System.currentTimeMillis() - intervalMillis(1, service); checkElapsedPreferenceName(prefsKeys.get(0)); checkElapsedTime(expectedElapsedTime, prefsValues.get(0)); checkClockPreferenceName(prefsKeys.get(1)); checkClockTime(expectedRealTime, prefsValues.get(1)); } catch (InterruptedException e) { e.printStackTrace(); } }