Mockito GregorianCalendate.getTime () throws an error

I am trying to make fun of the return of GregorianCalendar.getTime (), which should be Date (). But I get 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 tips to fix this? All help is much appreciated

+4
source share
2 answers

Take a look at the implementation getTime()that is in the superclass GregorianCalendarwith the name Calendar:

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

This means that you should probably try to make fun of getTimeInMillis():

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

+4
source

A GregorianCalendar - . . GregorianCalendar , .

0

Source: https://habr.com/ru/post/1526128/


All Articles