The problem is that the method GregorianCalendar.getTime()
is final, so Mockito cannot intercept the method call.
An alternative is to use Apache commons lang to transform the date to a Calendar
, so when you call getTime
it returns the value you expect
DateUtils.toCalendar(date)
If you want to go wild, you can use PowerMock to mock final fields, but I think it's not worth adding the complexity of PowerMock, when you have a simpler alternative.
And another point. You should try to avoid mocking objects that you don't own, this is quite an important point about unit testing and mock objects. Here's one reference that has some links about this practice.
And last, as it might apply to your project: it's quite a good practice to introduce a "Clock" object from where you can get the current time, and tests can manipulate this clock to return a "static" current time.
Edit
Java 8 includes the Clock abstraction, which has concrete implementations ideal for testing which can be obtained by calling Clock.fixed()
, Clock.tick()
, Clock.tickSeconds()
and Clock.tickMinutes()
... Or you can write your own implementation of Clock.