views:

43

answers:

2

Hi,

I need to do several integration tests on a Mongo database using Java, and I was looking for a DbUnit-like solution (DbUnit is for Hibernate) that can populate my database with custom data, and reset the state after each run.

Any tips?

Thanks

+1  A: 

Here's what I do: connect to a known (often shared) mongo instance, but create a new unique database for each test run using a UUID. You don't have to worry about creating collections, as they are created lazily when you store documents in them for the first time. Create any indexes you need in the constructor of the repository or DAO; mongo index creations succeed immediately without doing any work if the index already exists. Obviously, you don't need to worry about schema migrations ;-)

This scheme requires to you to start from an empty datastore, but it's a known state, so it's easy enough to populate in the setup phase of your tests if need be.

When the test is done, delete the entire database in the teardown phase.

Sean Reilly
+1  A: 

To start off, I don't know of any direct equivalent to DBUnit for Mongo. Mongo is still a new product, so you'll probably have to "roll your own" for some of this stuff.

However, there are several features of Mongo that should make this easy:

  1. It runs with minimal permissions
  2. It can simply "run" on prepared files
  3. It doesn't really have a schema (except for indexes)
  4. It can work of JSON data

Based on your dataset there are lots of ways to do this. But the basic tools are there.

  • You should be able to start a version specifically for your test, from your test.
  • You should be able to import "state" data from JSON file.
  • You should be able to apply any server-side functions from a JS file (from scratch).

So the whole thing should be pretty straightforward. Though you will have to write much of the glue code.

Gates VP
Dont forget, you can use Java's File.createTempFile (with file.deleteOnExit) to easily make test mongo db stores.
Steven