Mongodb + mockito不能一起工作?



我试图在我的java代码中为mongodb设置mock,并且我得到以下异常:

org.mockito.exceptions.misusing.WrongTypeOfReturnValue:
DBCursor$$EnhancerByMockitoWithCGLIB$$fc4f0e22 cannot be returned by getOptions()
getOptions() should return int

生成它的代码行是:

when(col.find(query)).thenReturn(cursor);

其中col是模拟DBCollection, query是模拟DBObject, cursor是模拟DBCursor。

我发现了以下非常模糊但可能相关的问题描述:

http://osdir.com/ml/mongodb-user/2010-08/msg02102.html

刚刚发现这个问题,find(DBObject obj)方法在DBCollection中是最终的:

http://grepcode.com/file/repo1.maven.org/maven2/org.mongodb/mongo-java-driver/2.1/com/mongodb/DBCollection.java

PowerMock最终解决了我的问题,因为它允许mock final方法。

最新更新