2017-01-12 52 views
3

这里是我的方案如何在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) 

我试着插入任何(),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。

回答

4

您传递原始值出现(如错误已经提到)。使用匹配代替如下:

import static org.mockito.Mockito.*; 

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

作为错误消息指出:

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

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