Testing with a real MongoDB database

Testing against an embedded MongoDB instance is quite handy. But there are times when we need to work with a real instance, and for multiple reasons: security settings, a batch of live data, a customized configuration. Whatever the reason, there is no need for that to derail our testing efforts.

We can write another test class, LiveImageRepositoryTests, and make it look like this:

    @RunWith(SpringRunner.class) 
    @DataMongoTest(excludeAutoConfiguration = 
      EmbeddedMongoAutoConfiguration.class) 
      public class LiveImageRepositoryTests { 
        @Autowired 
        ImageRepository repository;  
        @Autowired 
        MongoOperations operations; 

The details for this preceding live test are as follows:

  • @RunWith(SpringRunner.class) is our familiar ...

Get Learning Spring Boot 2.0 - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.