Mockito - 0 Matchers预期,1记录(InvalidUseOfMatchersException) [英] Mockito - 0 Matchers Expected, 1 Recorded (InvalidUseOfMatchersException)

查看:940
本文介绍了Mockito - 0 Matchers预期,1记录(InvalidUseOfMatchersException)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图模拟一些mongo类,所以我不需要连接(相当标准的东西),但下面的代码给我的问题:

 时(dbCollection.find(ISA(DBObject.class)))thenReturn(dbCursor)。 

运行这个是我的:


org.mockito.exceptions.misusing.InvalidUseOfMatchersException:

使用参数匹配器无效

0预期的匹配项,1记录:

在... GridFileManagerTest.beforeClass(GridFileManagerTest.java:67)



如果匹配器与原始值组合,则可能会发生此异常:

//不正确:someMethod(anyObject(),raw String);



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

示例:

//正确:

someMethod(anyObject(),eq(String by matcher));



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


如果我要这样做:


$ (dbCollection.find(mock(DBObject.class)))。thenReturn(dbCursor); b $ b

  

它不再有这个问题。这似乎没有完成我想要的 - 我想要返回值,当方法被调用与DBObject类型的对象。



想法?

解决方案

我认为您的结果与 dbCollection 之间的结果是一致的不是Mockito-mock(或者你的方法是静态的或者最终的)。这意味着在不能使用的情况下使用匹配器;因此0匹配预期,1记录。


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:

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.

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.

Thoughts?

解决方案

I think your results are compatible with the result that would happen if dbCollection is 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".

这篇关于Mockito - 0 Matchers预期,1记录(InvalidUseOfMatchersException)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆