I have to develop some Junit tests for Java code using a MongoDB store. Is there any framework/library which permits me initializing a mock in-memory MongoDB server?
(The idea is to test only the code itself, that means, in any machine independently on if MongoDB is installed & running).
Thanks in advance!
fongo might be what you are looking for.
I wrote a MongoDB fake implementation in Java: mongo-java-server (see this answer).
We're actually working on such a test system and it's quite feasible. In our approach our test framework extends the standard test case class (JUnit in our case but TestNG seems more capable) that sets up and tears down the various database dependecies with each test using the following steps :
Test Suite Setup
1) Start mongod process (we use ProcessBuilder, store Process instance)
Test Setup :
2) Run mongo with a test specific .js file to produce initial data state
Test
3) Run test
Test Teardown
4) Drop database
Test Suite Teardown
5) Stop mongod process (process.destroy())
Since starting and stopping mongod is the only time consuming thing i'd strongly suggest doing this as little as possible. Preferably once for the entire test suite. Our stuff isn't finished yet but early results are positive. I don't think many alternatives are available. No mongo mock library is available at time of writing and mongod does not have a in-memory/embedded mode.
Not really, you have to do that kind of thing yourself in your application layers. If you use Morphia you can use any mock framework with your service layers you like (since the objects are just POJOs), but there is nothing at the db/driver level to help you out.
Many people just use a local dev. mongodb instance with a set of test data since it is so fast. I know of people who load test data for each test, like copying a database with fake/test data.
As part of your test fixture setup, drop the database and populate it with any default test data.