ScheduledExecutorServiceMock implements ScheduledExecutorService { private ScheduledFutureMock<Void> scheduleRunnable( final Runnable runnable, final long initialDelay, final long delay, final TimeUnit unit, final ExcecutionType exceutionType) { final ScheduledFutureMock<Void> result = new ScheduledFutureMock<Void>( this, exceutionType, new CallableAdapter<Void>(runnable, null), initialDelay, delay, unit, systemTimeProvider); scheduleEvent(result); return result; } ScheduledExecutorServiceMock();  ScheduledExecutorServiceMock(final ISystemTimeProvider systemTimeProvider); List<ScheduledFutureMock<?>> getEvents(); List<ScheduledFutureMock<?>> executeEvents(); ScheduledFutureMock<?> executeNextEvent(); ScheduledFutureMock<?> offerNextEventForExecution(); @Override void execute(final Runnable runnable); @Override ScheduledFutureMock<Void> submit(final Runnable runnable); @Override ScheduledFutureMock<T> submit(final Runnable runnable, final T result); @Override ScheduledFutureMock<T> submit(final Callable<T> callable); @Override ScheduledFutureMock<Void> schedule(final Runnable runnable, final long delay, final TimeUnit unit); @Override ScheduledFutureMock<T> schedule(final Callable<T> callable, final long delay, final TimeUnit unit); @Override ScheduledFutureMock<Void> scheduleAtFixedRate(
        final Runnable runnable,
        final long initialDelay,
        final long period,
        final TimeUnit unit); @Override ScheduledFutureMock<Void> scheduleWithFixedDelay(
        final Runnable runnable,
        final long initialDelay,
        final long delay,
        final TimeUnit unit); @Override T invokeAny(final Collection<? extends Callable<T>> tasks); @Override T invokeAny(final Collection<? extends Callable<T>> tasks, final long timeout, final TimeUnit unit); @Override List<Future<T>> invokeAll(final Collection<? extends Callable<T>> tasks, final long timeout, final TimeUnit unit); @Override List<Future<T>> invokeAll(final Collection<? extends Callable<T>> tasks); @Override void shutdown(); @Override List<Runnable> shutdownNow(); @Override boolean isShutdown(); @Override boolean isTerminated(); @Override boolean awaitTermination(final long timeout, final TimeUnit unit);  }
@Test public void testScheduleRunnable() { final long delay = 1000; final SystemTimeProviderMock timeProvider = new SystemTimeProviderMock(); final ScheduledExecutorServiceMock executor = new ScheduledExecutorServiceMock(timeProvider); final Runnable runnable = Mockito.mock(Runnable.class); final ScheduledFutureMock<Void> futureResult = executor.schedule(runnable, delay, TimeUnit.MILLISECONDS); Mockito.verify(runnable, Mockito.never()).run(); Assert.assertEquals(1, executor.getEvents().size()); executor.executeEvents(); Mockito.verify(runnable, Mockito.never()).run(); Assert.assertEquals(1, executor.getEvents().size()); timeProvider.elapsedMillis(delay / 2); executor.executeEvents(); Mockito.verify(runnable, Mockito.never()).run(); Assert.assertEquals(1, executor.getEvents().size()); timeProvider.elapsedMillis(delay / 2); executor.executeEvents(); try { Assert.assertNull(futureResult.get(TIMEOUT, TimeUnit.MILLISECONDS)); Assert.assertTrue(futureResult.isDone()); Assert.assertFalse(futureResult.isCancelled()); Mockito.verify(runnable, Mockito.times(1)).run(); Assert.assertEquals(0, executor.getEvents().size()); } catch (final Exception e) { throw new RuntimeException(e); } }