I thought I’d share a technique I’ve found very useful for any application of significant size with a need for automated testing. A lot of transaction processing tasks are time sensitive, particularly in the financial or insurance domains.
This can make automated testing brittle or even impossible while your application directly acquires the current time or date from a system clock. The tip is to implement an application clock abstraction which is always used by your application for obtaining the current date and time. Thus in any testing, your application clock can be reliably used to simulate any arbitrary time and date repeatably. It’s a simple but invaluable pattern making test results repeatable and consistent for years if necessary.
This pattern is really simple to implement and is even relevant for database only operations as the same pattern can be applied there too.
It’s usually worth insuring your application clock can only simulate a date and time when in development environments. It would be nasty to have a production system accidentally start using test values for time and date.