Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mockito GregorianCalendate.getTime() causing error

I am trying to mock the return of GregorianCalendar.getTime() which should be a Date(). But am getting this error

org.mockito.exceptions.misusing.WrongTypeOfReturnValue: 
Date$$EnhancerByMockitoWithCGLIB$$91e3d4b7 cannot be returned by getTimeInMillis()
getTimeInMillis() should return long

Mockito.when(gregorianCalendar.getTime()).thenReturn(date);

Both gregorianCalendar and date are mocked objects.

Any advice on how to fix this? All help much appreciated

like image 850
Droid_Interceptor Avatar asked Feb 10 '14 09:02

Droid_Interceptor


1 Answers

Take a look on implementation of getTime() that is located in super class of GregorianCalendar named Calendar:

public final Date getTime() {
    return new Date(getTimeInMillis());
}

This means that you should probably try to mock getTimeInMillis() instead:

Mockito.when(gregorianCalendar.getTimeInMillis()).thenReturn(date.getTime());

like image 163
AlexR Avatar answered Sep 22 '22 11:09

AlexR