Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing Quartz CronTrigger trigger

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.

like image 645
Robert Munteanu Avatar asked Jun 29 '09 21:06

Robert Munteanu


People also ask

How does quartz trigger work?

Quartz scheduler allows an enterprise to schedule a job at a specified date and time. It allows us to perform the operations to schedule or unschedule the jobs. It provides operations to start or stop or pause the scheduler. It also provides reminder services.

Does quartz use cron?

Using Cron TriggersQuartz supports Cron-like expressions for specifying timers in a handy format.

What is Quartz cron expression?

A Cron Expressions quartz. Trigger. A cron expression is a string consisting of six or seven subexpressions (fields) that describe individual details of the schedule. These fields, separated by white space, can contain any of the allowed values with various combinations of the allowed characters for that field.

How do I stop quartz running?

deleteJob(jobKey(<JobKey>, <JobGroup>)); This method will only interrupt/stop the job uniquely identified by the Job Key and Group within the scheduler which may have many other jobs running.


1 Answers

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.

like image 61
skaffman Avatar answered Sep 23 '22 13:09

skaffman