I would rather try to hide this functionality behind a mocked interface, so that the "real" implementation will get the system date, and your mock implementation will return any date that you configured.
Passing the system date in unit tests is not a very nice thing for me - I will try to avoid it if I can. Unit tests should be as context sensitive as possible - this saves a ton of problems in the long run.
However, if you cannot change the code you are testing, you may not have another way. Then the question arises: are you only interested in the date or the full timestamp? The latter case sounds completely hopeless to me (in any other way than the aforementioned interface layout), since I think that just resetting the date before running your tests is not enough - you need a fixed point in time that will be returned throughout your unit tests. Perhaps your tests will go fast enough to finish until the clock starts ticking, maybe not - maybe they pass right now, and then begin to happen by chance some time after adding the following test: - (
If you only need a date, you can try changing the date in the @BeforeClass method and then reset it to @AfterClass , although this is rude.
source share