Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mockito can't match overloaded method

Tags:

java

mockito

I am trying to mock a SimpleDateFormat object but Mockito says I have the wrong number of args. Code:

 SimpleDateFormat spyDateFormat = spy(new SimpleDateFormat(DateFormatManager.MAIN_ACTIVITY_TITLE_FORMAT));
 // exception points to below line
 when(spyDateFormat.format(any(Date.class))).thenReturn("foo format");

exception:

org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
3 matchers expected, 1 recorded:

The method exists in DateFormat, its parent:

http://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html#format(java.util.Date)

Fiddled around with switching to DateFormat, using spy. No luck.

like image 550
browep Avatar asked Dec 07 '25 02:12

browep


1 Answers

The format(Date) method is final, so Mockito cannot spy it.

like image 190
Mureinik Avatar answered Dec 08 '25 14:12

Mureinik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!