You do not need a separate library for testing the time code - you just need to treat the "current time provider" as a dependency, like you do anything else. Therefore, avoid calls to new Date() , Calendar.getInstance() , etc. In Java and DateTime.Now , DateTime.UtcNow , etc. In .NET Instead, create a Clock or IClock to get the "current time", and then create a normal implementation that uses the system clock and a fake implementation that allows you to set the time. These are pretty little things to write and no third-party libraries are needed.
Embed the clock in any code that it needs, like any other dependency, and then you can easily find a fake test.
In fact, in Java 8 you can use the java.time API (which is much better than using Date and Calendar ) and has an abstract Clock class that also includes the idea of ββa time zone. In .NET, I would probably write a simple an interface and implementation based on DateTime , but always used the UTC type DateTime . Or use my Noda Time project , which comes with an IClock interface and a test version.
source share