问题描述
假设我有一个与
<bean id="midMonthCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="reminderJobDetail" />
<property name="cronExpression" value="0 0 6 15W * ?" />
</bean>
类似的CronTriggerBean
<bean id="midMonthCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="reminderJobDetail" />
<property name="cronExpression" value="0 0 6 15W * ?" />
</bean>
测试此bean实际上将在其指定日期即在最接近每个月15日的凌晨6点触发的最佳方法是什么?
更新:这应该是单元测试,所以我不会启动虚拟机或更改系统时间.
首先,测试CronTriggerBean
本身没有意义.它是spring框架的一部分,并且已经过测试.
更好的测试方法可能是测试您的cron表达式是否符合您的期望.这里的一种选择是使用Quartz的CronExpression
类.给定一个CronExpression
对象,您可以调用getNextValidTimeAfter(Date)
,该表达式将在给定日期之后的下一次触发表达式时返回.
Assuming that I have a CronTriggerBean
similar to
<bean id="midMonthCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="reminderJobDetail" />
<property name="cronExpression" value="0 0 6 15W * ?" />
</bean>
What is the best way to test that this bean will actually trigger at its specified date, i.e. on the weekday closest to the 15th of each month at 6 AM?
Update: This is supposed to be an unit test, so I'm not going to fire up a VM or change the system time.
Well firstly, there's no point in testing CronTriggerBean
itself. It's part of the spring framework, and has already been tested.
A better test might be to test that your cron expression is what you expect. One option here is to use Quartz's CronExpression
class. Given a CronExpression
object, you can call getNextValidTimeAfter(Date)
, which returns the next time after the given Date when the expression will fire.
这篇关于测试Quartz CronTrigger触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!