Here is a simple but slightly raw usage that can set the db state in json: https://github.com/kirilldev/mongomery
To load the state of a database, you only need to write two lines of code:
//db here is a com.mongodb.DB instance MongoDBTester mongoDBTester = new MongoDBTester(db); mongoDBTester.setDBState("predefinedTestData.json");
To check the status of db:
mongoDBTester.assertDBStateEquals("expectedTestData.json");
There are two ways to write json files with expected data:
Strict coincidence. This is a regular json file representing the state of db. In most cases, you do not need a more accurate description of the state of db after the test.
Matching pattern. If you want to use random strings in your test or, for example, your business logic generates random identifiers for objects, you can do a little more than strict matching:
{"Movies": [{"_id": "$ anyObject ()", "name": "Titanic", "year": 1997}]}
json above says the test expects that one document in the Movies collection will have the name Titanic and year 1997. It should also have a non-zero _id field with any object in it.
source share