title: Testing @Scheduled in spring
body: <p>Spring offers the possibility to schedule and execute tasks at specific intervals using annotations, e.g. <a href="http://static.springsource.org/spring/docs/3.0.x/reference/scheduling.html" rel="noreferrer"><code>@Scheduled</code></a></p>
  
  <p>Is there a convenient way to unit test this behavior?</p>
  
  <p>Of course I could call the method of the bean myself, but I want to make sure I don't run into problems like <a href="https://stackoverflow.com/questions/14242310/java-spring-scheduled-tasks-executing-twice">multiple executions due to misconfiguration</a> and so on.</p>
  
  <p>Other frameworks offer the possibility to fast forward the time yourself. One example is <a href="http://www.activiti.org/" rel="noreferrer">Activiti</a> where you can call</p>
  
  <pre><code>org.activiti.engine.impl.util.ClockUtil.setCurrentTime(date)
  </code></pre>
  
  <p>to fast forward the time used by the framework.</p>
  
  <p>Is there something comparable in Spring?</p>
  
  <p>Essentially what I want to do is something like this in a unit test (run using <code>SpringJUnit4ClassRunner</code>)</p>
  
  <pre><code>@Test public void testTaskScheduling() {
  
    assertThat(someOtherBean.getSomeProperty(), is(equalTo(whatIinitiallyExpect)));
  
    SpringClockUtil.setDate(dateInTwoHours)// This is what I am missing
    SpringTaskExecutor.executeAllScheduledTasks() // Also missing
  
    assertThat(someOtherBean.getSomeProperty(), is(equalTo(whatIexpectNow)));
  }
  </code></pre>