I want to pre-populate MongoDB database in Maven Java project with some data for integration testing purposes. I can easily achieve this by executing script in mongo shell manually (./mongo server:port/database --quiet my_script.js
), but how can I do this using Maven?

- 317
- 4
- 15
-
2Check this http://stackoverflow.com/questions/3491937/i-want-to-execute-shell-commands-from-mavens-pom-xml – Vinit Prajapati Oct 17 '12 at 07:17
-
Thanks a lot! But how about portability? If one of our developers uses Windows (and can't execute shell script) what should I do? – Ivan Sharamet Oct 17 '12 at 07:57
3 Answers
Is the MongoDB database running before the tests are run? Or do you start the server as part of your tests?
In either case, I think it would be possible to use dbunit maven plugin to load an external file with testdata. Take a look at http://mojo.codehaus.org/dbunit-maven-plugin/operation-mojo.html

- 86
- 2
-
1Thanks, but I dont think that DBUnit is able to work with MongoDB database. – Ivan Sharamet Oct 17 '12 at 09:37
-
1Ok, I figured it had a standard jdbc driver. Maybe NoSQLUnit might be something then: http://www.lordofthejars.com/2012/06/nosqlunit-030-released.html – Joakim Z Oct 17 '12 at 09:41
-
Thank you! It looks like NoSQLUnit is under active development and I definitely give it a try! – Ivan Sharamet Oct 17 '12 at 09:53
You could use the Mongo Java API to run your script into the database with the DB.doEval()
method. If you already have a running database then connecting and running the script could be done as part of your test setup.
That said, for some of my projects, I am using Maven with embedmongo Maven plugin, which allows a mongo database to be created on the fly for integration testing. That, combined with your script might be an alternate solution that does not rely on an existing Mongo database to be running.

- 43,435
- 12
- 121
- 150
-
Thank you! It seems to be the right solution. And (if it matters) I use Spring Data for MongoDB in my project, but I can easily access underlying API. – Ivan Sharamet Oct 17 '12 at 10:31
You can now use embedmongo-maven-plugin to achieve this. It has a mongoimport
goal to run mongoimport or a mongo-scripts
goal to eval arbitrary js scripts.

- 11,665
- 2
- 41
- 54