测试Quartz CronTrigger触发器 [英] Testing Quartz CronTrigger trigger

查看:525
本文介绍了测试Quartz CronTrigger触发器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个与

<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触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆