mockito 无法匹配重载方法

bro*_*wep 3 java mockito

我正在尝试模拟一个SimpleDateFormat对象,但 Mockito 说我的参数数量错误。代码:

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

例外:

org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
3 matchers expected, 1 recorded:
Run Code Online (Sandbox Code Playgroud)

该方法存在于其父 DateFormat 中:

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

尝试切换到 DateFormat,使用spy. 没有运气。

Mur*_*nik 5

format(Date)方法是final,因此 Mockito 无法监视它。