java Mockito - 0 匹配器预期,1 记录 (InvalidUseOfMatchersException)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11458963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:11:32  来源:igfitidea点击:

Mockito - 0 Matchers Expected, 1 Recorded (InvalidUseOfMatchersException)

javaexceptionmockingmockito

提问by Paul

I'm trying to mock up some mongo classes so that I don't need a connection (fairly standard stuff) but the following code gives me problems:

我正在尝试模拟一些 mongo 类,这样我就不需要连接(相当标准的东西),但以下代码给我带来了问题:

when(dbCollection.find(isA(DBObject.class))).thenReturn(dbCursor);

Running this get's me:

运行这个让我:

org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
0 matchers expected, 1 recorded:
at ...GridFileManagerTest.beforeClass(GridFileManagerTest.java:67)

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.

org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
参数匹配器的使用无效!
0 个匹配器预期,1 个记录:
在 ...GridFileManagerTest.beforeClass(GridFileManagerTest.java:67)

如果匹配器与原始值组合,则可能发生此异常:
//incorrect: someMethod(anyObject(), "raw String");

使用匹配器时,所有参数都必须由匹配器提供。
例如:
//正确:
someMethod(anyObject(), eq("String by matcher"));

有关更多信息,请参阅 Matchers 类的 javadoc。

If I were to do this though:

如果我要这样做:

when(dbCollection.find(mock(DBObject.class))).thenReturn(dbCursor);

it no longer has that problem. This doesn't seem to accomplish what I want though - I want to return the value when the method is called with an object of type DBObject.

它不再有这个问题。这似乎并没有完成我想要的 - 我想在使用 DBObject 类型的对象调用该方法时返回该值。

Thoughts?

想法?

回答by Kevin Welker

I think your results are compatible with the result that would happen if dbCollectionis not a Mockito-mock (or your method is static or final). That would mean that a matcher is being used where none can be used; hence the "0 matchers expected, 1 recorded".

我认为你的结果与如果dbCollection不是 Mockito-mock(或者你的方法是静态的或最终的)会发生的结果是兼容的。这意味着在无法使用匹配器的地方使用匹配器;因此“0 个匹配器预期,1 个记录”。

回答by micseydel

This same issue can be reproduced in Scala if you have default arguments. It may look like you're providing any() for every argument, but you should verify that the method definition doesn't have any default parameters which might be messing things up.

如果您有默认参数,同样的问题可以在 Scala 中重现。看起来您似乎为每个参数提供了 any(),但您应该验证方法定义没有任何默认参数,这可能会使事情变得一团糟。

回答by ben_joseph

Probably unrelated, but I encountered the same error when I spied a package private method. Changing it to public solved the issue for me.

可能无关,但是当我监视包私有方法时遇到了同样的错误。将其更改为 public 为我解决了这个问题。