Jay Fields is a software developer at DRW Trading. He has a passion for discovering and maturing innovative solutions. His most recent work has been in the Domain Specific Language space where he's delivered applications that empowered subject matter experts to write the business rules of the applications. He is also very interested in maturing software design through software testing. Jay is a DZone MVB and is not an employee of DZone and has posted 116 posts at DZone. You can read more from them at their website. View Full User Profile

Freezing Joda Time

06.23.2009
| 8043 views |
  • submit to reddit
Once upon a time Mark Needham wrote about freezing Joda Time. Mark gives all the important details for freezing time (which is often helpful for testing), but I came up with some additional code that I like to add on top of his example.

Two things bother me about Mark's example. First of all, I always like the last line of my test to be the assertion. It's not a law, but it is a guideline I like to follow. Secondly, I don't like having to remember that I need to reset the time back to following the system clock.

I came up with the following idea. It's definitely a poor man's closure, but it does the job for me.
    
@Test
public void shouldFreezeTime() {
Freeze.timeAt("2008-09-04").thawAfter(new Snippet() {{
assertEquals(new DateTime(2008, 9, 4, 1, 0, 0, 0), new DateTime());
}});
}

The Freeze class is very simple:
    public class Freeze {

public static Freeze timeAt(String dateTimeString) {
DateTimeUtils.setCurrentMillisFixed(JodaDateTime.create(dateTimeString).getMillis());
return new Freeze();
}

public void thawAfter(Snippet snippet) {
DateTimeUtils.setCurrentMillisSystem();
}
}

The Snippet class is even more simple:
    public class Snippet {}


Using this code I can keep my assertions as close to the end of the test method as possible, and it's not possible to forget to reset the time back to the system clock.

From http://blog.jayfields.com/

0
Your rating: None
Published at DZone with permission of Jay Fields, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Walter Laan replied on Tue, 2009/06/23 - 2:28am

Assuming your talking about JUnit, isn't the tearDown() / @After annotation meant for that?

sub online replied on Fri, 2009/06/26 - 2:32am

I don't like scary and violent movies. There's too much blood and gore.links of londonNot bad.I'll do whatever you want.But just don't bother me anymore.Stop picking on me.Be quiet.links of londonScum of society.What does it matter!You don't have the right. Who do you think you are? You don't have the right to say those things.Don't make me laugh.Tiffany JewelleryMy heart hurts.I got you! I caught you.You have to be on time. Don't be late.You think this is funny!

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.