Spring 提供了使用注释以特定时间间隔安排和执行任务的可能性,例如@Scheduled
有没有一种方便的方法来单元测试这种行为?
当然我可以自己调用 bean 的方法,但是我想确保我不会因为配置错误等而遇到多次执行等问题。
其他框架提供了自己快进时间的可能性。一个例子是Activiti,您可以在其中调用
org.activiti.engine.impl.util.ClockUtil.setCurrentTime(date)
快进框架使用的时间。
春天有什么可比的吗?
基本上我想要做的是在单元测试中这样的事情(使用运行SpringJUnit4ClassRunner
)
@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)));
}