这是我的方案
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)
我尝试插入any(),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.
我们不使用powermocks。
答案 0 :(得分:9)
您在那里传递原始值(如已提到的错误)。使用matcher代替:
import static org.mockito.Mockito.*;
...
when(barObj.bar(eq(10), any(Date.class))
.thenReturn(10)
答案 1 :(得分:2)
如错误消息所示:
使用匹配器时,所有参数都必须由匹配器提供。
Bar bar = mock(Bar.class)
when(bar.bar(eq(10), anyObject())).thenReturn(10)