« Return to Thread: Testing code that depends on today's date

Testing code that depends on today's date

by Per Jacobsson :: Rate this Message:

Reply to Author | View in Thread

Hi,
This has been annoying me for a while and I can't think of a clean solution for it. Hope this makes sense: I have some code that returns different results depending on what today's date is (the number of days since a user first registered, etc). The simple approach is to just create a new instance of java.util.Date whenever I need to know the current date, but that tends to make both unit and automated functional testing difficult.

I'm currently injecting today's date into the classes that need it, and then the tests can just use a static date. It gets pretty ugly though, a lot of times this date has to be dragged along deep into the domain model.

An alternative would be to take the old school approach and have a static factory that everything that needs to know today's date calls, and allow the unit tests to override with a test date. Of course, then you run into all the other issues that dependency injection solves.

Any thoughts?
Thanks,
/ Per



[Non-text portions of this message have been removed]

 « Return to Thread: Testing code that depends on today's date

LightInTheBox - Buy quality products at wholesale price!