title: The easiest way to configure Embedded MongoDB
body: <p>I'm wondering, is there any example how to properly configure embedded <code>MongoDB</code> with <code>Spring Boot</code>?</p>
  
  <p>For example, this is how i configure H2 embedded database:</p>
  
  <pre><code>import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.context.annotation.Profile;
  import org.springframework.context.annotation.PropertySource;
  import org.springframework.core.env.Environment;
  import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
  import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
  import org.springframework.jndi.JndiObjectFactoryBean;
  
  import javax.sql.DataSource;
  
  
  @Configuration
  @PropertySource({"configs/datasource.properties"})
  public class DataSourceConfig {
  
      @Bean
      @Profile("test")
      public DataSource testDataSource() {
          return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).build();
      }
  }
  </code></pre>
  
  <p>And this works well, but there is a problem. This approach doesnt provide configuration with MongoDB.</p>
  
  <p>Are there any workarounds?</p>
  
  <p><strong>UPDATE 1:</strong></p>
  
  <blockquote>
    <p>[ERROR] Failed to execute goal
    org.springframework.boot:spring-boot-maven-plugin:1.5.6.RELEASE:run
    (default-cli) on project XXX: An exception occurred while
    running. null: InvocationTargetException: Error creating bean with
    name 'mongoTemplate' defined in class path resource
    [com/reborn/XXX/config/DataSourceConfig .class]: Bean
    instantiation via factory method failed; nested exception is
    org.springframework.beans.BeanInstantiationException: Failed to
    instantiate [org.springframewor k.data.mongodb.core.MongoTemplate]:
    Factory method 'mongoTemplate' threw exception; nested exception is
    java.lang.NoClassDefFoundError: de/flapdoodle/embed/mongo/distribu
    tion/IFeatureAwareVersion:
    de.flapdoodle.embed.mongo.distribution.IFeatureAwareVersion -> [Help
    1]</p>
  </blockquote>
  
  <p><strong>UPDATE 2:</strong></p>
  
  <p><strong>pom.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
      &lt;groupId&gt;com.reborn&lt;/groupId&gt;
      &lt;artifactId&gt;xxx&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
      &lt;packaging&gt;jar&lt;/packaging&gt;
  
      &lt;name&gt;xxx&lt;/name&gt;
      &lt;description&gt;Demo project for Spring Boot&lt;/description&gt;
  
      &lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;1.5.6.RELEASE&lt;/version&gt;
          &lt;relativePath/&gt; &lt;!-- lookup parent from repositories --&gt;
      &lt;/parent&gt;
  
      &lt;properties&gt;
          &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
          &lt;project.reporting.outputEncoding&gt;UTF-8&lt;/project.reporting.outputEncoding&gt;
          &lt;java.version&gt;1.8&lt;/java.version&gt;
      &lt;/properties&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-data-mongodb&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;de.flapdoodle.embed&lt;/groupId&gt;
              &lt;artifactId&gt;de.flapdoodle.embed.mongo&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;cz.jirutka.spring&lt;/groupId&gt;
              &lt;artifactId&gt;embedmongo-spring&lt;/artifactId&gt;
              &lt;version&gt;1.3.1&lt;/version&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  
  &lt;/project&gt;
  </code></pre>
  
  <p><strong>DatasourceConfig:</strong></p>
  
  <pre><code>package com.reborn.xxx.config;
  
  import com.mongodb.MongoClient;
  import cz.jirutka.spring.embedmongo.EmbeddedMongoFactoryBean;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.data.mongodb.core.MongoTemplate;
  
  import java.io.IOException;
  
  @Configuration
  public class DataSourceConfig {
  
      @Bean
      public MongoTemplate mongoTemplate() throws IOException {
          EmbeddedMongoFactoryBean mongo = new EmbeddedMongoFactoryBean();
          mongo.setBindIp("localhost");
          MongoClient mongoClient = mongo.getObject();
          MongoTemplate mongoTemplate = new MongoTemplate(mongoClient, "abcd");
          return mongoTemplate;
      }
  
  }
  </code></pre>
  
  <p><strong>UPDATE 3:</strong></p>
  
  <blockquote>
    <p>[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.6.RELEASE:run (default-cli) on project starbucks-finder: An exception occurred while
    running. null: InvocationTargetException: Error creating bean with name 'mongoTemplate' defined in class path resource [com/reborn/xxx/config/DataSourceConfig
    .class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframewor
    k.data.mongodb.core.MongoTemplate]: Factory method 'mongoTemplate' threw exception; nested exception is java.lang.IllegalArgumentException: this version does not support
    32Bit: PRODUCTION:Windows:B32 -> [Help 1]</p>
  </blockquote>