• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

How to test a MongoDb DAO class. Mocking?

 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a simple MongoDB DAO class for implementing basic CRUD operations (e.g. connection to the running instance and database, etc), but I am struggling to understand how to test it. Are mocks (Mockito) recommended? Can you provide an example of how they can be used?

Thank you for your help,

I.
 
Sheriff
Posts: 16933
285
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do you think mocks are going to help you?

Mock objects are used as "stand-ins" for the real things so that you can test something that interacts with them. The mocks are merely there to simulate the behavior of the more unwieldy things they stand in for. In the case of DAOs, the thing you are testing is usually the class that uses the DAO. A DAO is normally an interface, which makes it easier to mock out.

Those are just examples of how you might define classes based on a DAO interface.

This is an example of how you might unit test a class that uses TwitterFeedDao:

If you are going to unit test a MongoDBDao, then you'd have to mock out the connection and anything else that represents MongoDB functionality. I wouldn't bother with that though. My approach with DAOs is to use integration tests with them. Spring has many wonderful features to help write integration tests. Google for "integration testing with Spring and MongoDB".

See also: http://docs.mockito.googlecode.com/hg/1.9.5/org/mockito/InjectMocks.html
 
Ranch Hand
Posts: 258
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would this one helps you ? https://github.com/fakemongo/fongo
 
You can't expect to wield supreme executive power just because
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic