JodaTime goodies – testing time dependent logic

This post outlined a possible way to test time-dependent logic by abstracting the concept of current time behind a clock interface and providing a mock clock implementation for the tests. This is important because otherwise relying on the system clock to test, say, if a particular action is triggered every day at 12.00 might be tricky.

JodaTime makes it easier to implement the above. It already provides a MillisProvider interface (to abstract the concept of current time) which can be accessed through methods from the DateTimeUtils class, thus saving you from writing your own.


//fix the current time to 1000 millis
DateTimeUtils.setCurrentMillisFixed(1000);

... run time-dependent logic

//time is still at 1000 millis
long time = DateTimeUtils.getCurrentMillis();

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s