This guide will show you a nifty trick to ease your testing in projects using MongoDB in Spring Boot.
Many solutions seem to appear when you search for this, but the simplest solution seems to be the build in auto-configuration for Flapdoodle's embedded Mongo.
We used Maven for our project, and added the following dependency to the pom.xml file:
<dependency> <groupId>de.flapdoodle.embed</groupId> <artifactId>de.flapdoodle.embed.mongo</artifactId> <version>1.50.2</version> <scope>test</scope> </dependency>
The newest version number can be found at Flapdoodle's howto on GitHub (go there for updates), but the scope has been added. Setting the scope to
test makes it available during tests but not during regular runtime.
Now you should be ready to run your tests without mixing your production database into it.