public interface DateUtil { public long currentTimeMillis(); // ... }
An having the class that implements nextMinuteFromNow() be injected with a implmentation of DateUtil, and using it instead of System.currentTimeMillis().
This way, durring test, you could inject an implementation of DateUtil that does whatever you expect.
I understand what you're doing, but I do have to ask: Why would you unit test the first function? All you're testing is that the Date API works correctly, which, as a testing pattern, is a violation of encapsulation.
@Conrad, the value is in gaining confidence in your assumptions. Have you worked with the Date API? It's a mess. But worst of all, you can't test it, which is the point of the article.
Okay this is a year and a half old, but a reply is still worthwhile.
Could you give an example for the mutable singleton solution?
ReplyDeleteCreating an interface
ReplyDeletepublic interface DateUtil {
public long currentTimeMillis();
// ...
}
An having the class that implements nextMinuteFromNow() be injected with a implmentation of DateUtil, and using it instead of System.currentTimeMillis().
This way, durring test, you could inject an implementation of DateUtil that does whatever you expect.
I understand what you're doing, but I do have to ask: Why would you unit test the first function? All you're testing is that the Date API works correctly, which, as a testing pattern, is a violation of encapsulation.
ReplyDeletePossibly, you just meant it as a strawman.
@Conrad, the value is in gaining confidence in your assumptions. Have you worked with the Date API? It's a mess. But worst of all, you can't test it, which is the point of the article.
ReplyDeleteOkay this is a year and a half old, but a reply is still worthwhile.