Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6847269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:43:29+00:00 2026-05-27T00:43:29+00:00

My database of choice is MongoDB. I’m writing a data-layer API to abstract implementation

  • 0

My database of choice is MongoDB. I’m writing a data-layer API to abstract implementation details from client applications – that is, I’m essentially providing a single public interface (an object which acts as an IDL).

I’m testing my logic as I go in a TDD manner. Before each unit test, an @Before method is called to create a database singleton, after which, when the test completes, an @After method is called to drop the database. This helps to promote independence among unit tests.

Nearly all unit tests, i.e. performing a contextual query, require some kind of insertion logic to occur before hand. My public interface provides an insert method – yet, it seems incorrect to use this method as precursor logic to each unit test.

Really I need some kind of mocking mechanism, yet, I haven’t had much experience with mocking frameworks, and it seems that Google returns nothing re a mocking framework one might use with MongoDB.

What do others do in these situations? That is, how do people unit test code that interacts with a database?

Also, my public interface connects to a database defined in a external configuration file – it seems incorrect to use this connection for my unit testing – again, a situation that would benefit from some kind of mocking?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:43:29+00:00Added an answer on May 27, 2026 at 12:43 am

    As sbridges wrote in this post it is a bad idea not to have a dedicated service (sometimes also known as repository or DAO) which abstracts the data access from the logic. Then you could test the logic by providing a mock of the DAO.

    Another approach which I do is to create a Mock of the Mongo object (e.g. PowerMockito) and then return the appropriate results.
    This because you don’t have to test if the database works in unit tests but more over you should test if the right query was sent to the databse.

    Mongo mongo = PowerMockito.mock(Mongo.class);
    DB db = PowerMockito.mock(DB.class);
    DBCollection dbCollection = PowerMockito.mock(DBCollection.class);
    
    PowerMockito.when(mongo.getDB("foo")).thenReturn(db);
    PowerMockito.when(db.getCollection("bar")).thenReturn(dbCollection);
    
    MyService svc = new MyService(mongo); // Use some kind of dependency injection
    svc.getObjectById(1);
    
    PowerMockito.verify(dbCollection).findOne(new BasicDBObject("_id", 1));
    

    That would also be an option. Of course the creation of the mocks and returning of the appropriate objects is just coded as an example above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a selectmultiple choice widget that gets data from mysql database and displays
Background I'm prototyping a conversion from our RDBMS database to MongoDB. While denormalizing, it
My understanding is that sqllite is pretty much the only choice for database on
How can i save the info from a multiple choice listview into the database?
I'd like to give customers a choice of the database engine, but also want
In almost all projects, the choice of the database system is 'almost' automatic ..
Database is OracleXE and here is the problem: data gets entered in tables UPS
Database has tables Photos and PhotoAlbums. I need a query that will select all
A database application that I'm currently working on, stores all sorts of settings in
I have an application that can not afford to lose data, so Postgresql is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.