O'Reilly logo

Learning Spring Boot 2.0 - Second Edition by Greg L. Turnquist

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Testing with embedded MongoDB

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:

 testCompile("de.flapdoodle.embed:de.flapdoodle.embed.mongo") ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required