用4个参数模拟一个方法(包括对象数组)

时间:2016-12-24 12:47:25

标签: java junit mockito

我试图使用Mockito模拟具有以下签名的方法:

getMessage(String arg0, Object[] arg1, String arg2, Locale arg3)

我尝试过使用多种匹配器组合,包括:

Mockito.when(messageSource.getMessage(any(),
                                      any(),
                                      any(), 
                                      any()
                                      )
            ).thenReturn(testString);

但是,都会产生以下错误:

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Invalid use of argument matchers!
3 matchers expected, 4 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.

我如何模仿这种方法?

1 个答案:

答案 0 :(得分:0)

Mockito.when(messageSource.getMessage(any(),
            any(Object[].class),
            any(),
            any()
            )
    ).thenReturn(testString);

应该做的伎俩