[Guide] Set up an embedded MongoDB for testing in Spring Boot

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.

An example of this can be found in our casestudy-user-service. The example also uses a config-server to resolve the production database host when not running tests.

Now you should be ready to run your tests without mixing your production database into it.