The first annotation listed above for slice testing is @DataMongoTest. In this section, we want to write some test methods that involve our MongoDB-specific code.
When it comes to testing MongoDB code, we have the following two options provided by Spring Boot:
- Testing against an embedded MongoDB instance
- Testing against a live MongoDB instance
Spring Boot, by default, will check if Flapdoodle, the embedded MongoDB database, is on the classpath. If so, it will attempt to run the test using it. If Flapdoodle is NOT on our classpath, it will attempt to connect to a real MongoDB instance.
So let's get started by adding flapdoodle to our project's list of dependencies as follows: