views:

1147

answers:

2

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.

A: 
  1. You can always wait until the 15h of July.
  2. Being more serious... If it's really a key part of the application and I you need to have it tested fully. I would recommend using some virtualization setups and have the application installed within some guest machine. Then you could play with the system clock and test different date/times without spending a whole month on it. Moreover there's nothing that should stop you from automating such tests.
Grzegorz Oledzki
Thanks for the answer. This is supposed to be an unit test, so I'm not going to fire up a VM or change the system time.
Robert Munteanu
+5  A: 

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.

skaffman
Just to clarify - I'm not suggesting you change your code to use CronExpression, but that your unit test should use it.
skaffman
Interesting, I'll take a look at it.
Robert Munteanu