如何在mockito中模拟日期?

Jav*_*per 9 java mockito

这是我的情景

public int foo(int a) {
   return new Bar().bar(a, new Date());
}

My test:
Bar barObj = mock(Bar.class)
when(barObj.bar(10, ??)).thenReturn(10)
Run Code Online (Sandbox Code Playgroud)

我尝试插入任何(),anyObject()等.任何想法插入什么?

但是我一直得到例外:

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


This exception may occur if matchers are combined with raw values:
    //incorrect:
    someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
    //correct:
    someMethod(anyObject(), eq("String by matcher"));

For more info see javadoc for Matchers class.
Run Code Online (Sandbox Code Playgroud)

我们不使用powermocks.

rko*_*egi 12

你在那里传递原始值(如已经提到的错误).使用matcher代替:

import static org.mockito.Mockito.*;

...
when(barObj.bar(eq(10), any(Date.class))
   .thenReturn(10)
Run Code Online (Sandbox Code Playgroud)