title: How do you configure Embedded MongDB for integration testing in a Spring Boot application?
body: <p>I have a fairly simple Spring Boot application which exposes a small REST API and retrieves data from an instance of MongoDB. Queries to the MongoDB instance go through a Spring Data based repository. Some key bits of code below.</p>
  
  <pre class="lang-java prettyprint-override"><code>// Main application class
  @EnableAutoConfiguration(exclude={MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})
  @ComponentScan
  @Import(MongoConfig.class)
  public class ProductApplication {
      public static void main(String[] args) {
          SpringApplication.run(ProductApplication.class, args);
      }
  }
  </code></pre>
  
  <pre class="lang-java prettyprint-override"><code>// Product repository with Spring data
  public interface ProductRepository extends MongoRepository&lt;Product, String&gt; {
  
      Page&lt;Product&gt; findAll(Pageable pageable);
  
      Optional&lt;Product&gt; findByLineNumber(String lineNumber);
  }
  </code></pre>
  
  <pre class="lang-java prettyprint-override"><code>// Configuration for "live" connections
  @Configuration
  public class MongoConfig {
  
      @Value("${product.mongo.host}")
      private String mongoHost;
  
      @Value("${product.mongo.port}")
      private String mongoPort;
  
      @Value("${product.mongo.database}")
      private String mongoDatabase;
  
      @Bean(name="mongoClient")
      public MongoClient mongoClient() throws IOException {
          return new MongoClient(mongoHost, Integer.parseInt(mongoPort));
      }
  
      @Autowired
      @Bean(name="mongoDbFactory")
      public MongoDbFactory mongoDbFactory(MongoClient mongoClient) {
          return new SimpleMongoDbFactory(mongoClient, mongoDatabase);
      }
  
      @Autowired
      @Bean(name="mongoTemplate")
      public MongoTemplate mongoTemplate(MongoClient mongoClient) {
          return new MongoTemplate(mongoClient, mongoDatabase);
      }
  }
  </code></pre>
  
  <pre class="lang-java prettyprint-override"><code>@Configuration
  @EnableMongoRepositories
  public class EmbeddedMongoConfig {
  
      private static final String DB_NAME = "integrationTest";
      private static final int DB_PORT = 12345;
      private static final String DB_HOST = "localhost";
      private static final String DB_COLLECTION = "products";
  
      private MongodExecutable mongodExecutable = null;
  
      @Bean(name="mongoClient")
      public MongoClient mongoClient() throws IOException {
          // Lots of calls here to de.flapdoodle.embed.mongo code base to 
          // create an embedded db and insert some JSON data
      }
  
      @Autowired
      @Bean(name="mongoDbFactory")
      public MongoDbFactory mongoDbFactory(MongoClient mongoClient) {
          return new SimpleMongoDbFactory(mongoClient, DB_NAME);
      }
  
      @Autowired
      @Bean(name="mongoTemplate")
      public MongoTemplate mongoTemplate(MongoClient mongoClient) {
          return new MongoTemplate(mongoClient, DB_NAME);
      }
  
      @PreDestroy
      public void shutdownEmbeddedMongoDB() {
          if (this.mongodExecutable != null) {
              this.mongodExecutable.stop();
          }
      }
  }
  </code></pre>
  
  <pre class="lang-java prettyprint-override"><code>@RunWith(SpringJUnit4ClassRunner.class)
  @SpringApplicationConfiguration(classes = TestProductApplication.class)
  @IntegrationTest
  @WebAppConfiguration
  public class WtrProductApplicationTests {
  
      @Test
      public void contextLoads() {
          // Tests empty for now
      }
  
  }
  </code></pre>
  
  <pre class="lang-java prettyprint-override"><code>@EnableAutoConfiguration(exclude={MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})
  @ComponentScan
  @Import(EmbeddedMongoConfig.class)
  public class TestProductApplication {
  
      public static void main(String[] args) {
          SpringApplication.run(TestProductApplication.class, args);
      }
  }
  </code></pre>
  
  <p>So the idea here is to have the integration tests (empty at the moment) connect to the embedded mongo instance and not the "live" one. However, it doesn't work. I can see the tests connecting to the "live" instance of Mongo, and if I shut that down the build simply fails as it is still attempting to connect to the live instance of Mongo. Does anyone know why this is? How do I get the tests to connect to the embedded instance?</p>