Java 模拟具有4个参数的方法(包括对象数组)
我正在尝试使用Mockito模拟具有以下签名的方法:Java 模拟具有4个参数的方法(包括对象数组),java,junit,mockito,Java,Junit,Mockito,我正在尝试使用Mockito模拟具有以下签名的方法: getMessage(String arg0, Object[] arg1, String arg2, Locale arg3) 我尝试过使用多种匹配器组合,包括: Mockito.when(messageSource.getMessage(any(), any(), any(),
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.
我怎么能嘲笑这种方法呢
Mockito.when(messageSource.getMessage(any(),
any(Object[].class),
any(),
any()
)
).thenReturn(testString);
应该做这个把戏发布一个完整的例子来重现这个问题(类被模拟+测试)。特别是如果在这一行上面有任何匹配器的使用,请添加它们;另外,请检查您模拟的
MessageSource
类和方法是否为非final
;我有一种预感,Mockito认为你在模仿一种不同于你试图模仿的方法,因为final
。是的,你是对的。碰巧,Locale
是一个final
类。这就是例外的原因。不,这不是例外的原因。String也是最后一个类。您在发布的代码中没有模仿区域设置。