开发者

Integration tests with MongoDB?

开发者 https://www.devze.com 2023-01-19 03:58 出处:网络
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,开发者_JAVA

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,开发者_JAVA技巧 and reset the state after each run.

Any tips?

Thanks


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.


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.


This question has been answered here and permits to start and stop an instance between each test: https://stackoverflow.com/a/9830861/82609

But start/stop between each test seems to slow down integration tests, and thus you'd better start/stop it for the whole test suite: https://stackoverflow.com/a/14171993/82609


I know this question is old, but maybe my answer will be useful for someone. Here is a simple util that I wrote it recently: https://github.com/kirilldev/mongomery

Very simple to populate db with data from json file:

//db here is a com.mongodb.DB instance
MongoDBTester mongoDBTester = new MongoDBTester(db);
mongoDBTester.setDBState("predefinedTestData.json");

To check db state:

mongoDBTester.assertDBStateEquals("expectedTestData.json");

It supports placeholders for expected files which can be useful in some situations.


You can use nosql-unit that has a MongoDB module

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号