如何在Mockito中模拟日期?

13

这是我的情境

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.

我们不使用 PowerMock。

2个回答

19

你在那里传递了原始值(正如错误已经提到的那样)。使用匹配器来替代,就像这样:

import static org.mockito.Mockito.*;

...
when(barObj.bar(eq(10), any(Date.class))
   .thenReturn(10)

2

正如错误信息所述:

使用匹配器时,所有参数都必须由匹配器提供。

Bar bar = mock(Bar.class)
when(bar.bar(eq(10), anyObject())).thenReturn(10)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接