ScheduledExecutorServiceMock implements ScheduledExecutorService { @Override public ScheduledFutureMock<Void> scheduleAtFixedRate( final Runnable runnable, final long initialDelay, final long period, final TimeUnit unit) { return scheduleRunnable(runnable, initialDelay, period, unit, ExcecutionType.FIXED_RATE); } 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(expected = CancellationException.class) public void testScheduleAtFixedRate() throws TimeoutException, InterruptedException, ExecutionException { final long initialDelay = 1000; final long period = 200; final long runDuration = 20; final SystemTimeProviderMock timeProvider = new SystemTimeProviderMock(); final ScheduledExecutorServiceMock executor = new ScheduledExecutorServiceMock(timeProvider); final Runnable runnable = Mockito.mock(Runnable.class); final Runnable timeConsumingRunnable = new Runnable() { @Override public void run() { timeProvider.elapsedMillis(runDuration); runnable.run(); } }; final ScheduledFutureMock<Void> futureResult = executor .scheduleAtFixedRate(timeConsumingRunnable, initialDelay, period, 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()); int expectedInvocations = 0; timeProvider.elapsedMillis(initialDelay / 2); executor.executeEvents(); Mockito.verify(runnable, Mockito.times(expectedInvocations)).run(); Assert.assertEquals(1, executor.getEvents().size()); timeProvider.elapsedMillis(initialDelay / 2); executor.executeEvents(); Mockito.verify(runnable, Mockito.times(++expectedInvocations)).run(); Assert.assertEquals(1, executor.getEvents().size()); for (int i = 0; i < 5; i++) { timeProvider.elapsedMillis((period - runDuration) / 2); executor.executeEvents(); Mockito.verify(runnable, Mockito.times(expectedInvocations)).run(); Assert.assertEquals(1, executor.getEvents().size()); timeProvider.elapsedMillis((period - runDuration) / 2); executor.executeEvents(); Mockito.verify(runnable, Mockito.times(++expectedInvocations)).run(); Assert.assertEquals(1, executor.getEvents().size()); } futureResult.cancel(false); try { futureResult.get(TIMEOUT, TimeUnit.MILLISECONDS); } finally { Assert.assertEquals(0, executor.getEvents().size()); } }