简体   繁体   中英

Pre-populate MongoDB database with Maven

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?

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM