Mongodb + mockito not working together?

I am trying to configure mocks for mongodb in my java code and I get the following exception:

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

The line of code that generates this:

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

If col is the mocked DBCollection, the query is the mocked DBObject and the cursor is the mocked DBCursor.

I found the following very vague, but probably relevant description of the problem:

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

+4
source share
1 answer

Just found a problem, the find (DBObject obj) method is final in DBCollection:

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

PowerMock ultimately solved my problem as it allows you to taunt the final methods.

+4
source

Source: https://habr.com/ru/post/1379728/


All Articles