title: How to make the junit tests use the embedded mongoDB in a springboot application?
body: <p>I am learning springboot and have created a simple springboot application. I want it to use the embedded mongoDB when it runs the unit tests and the external mongoDB for the rest of the application. However it uses the external mongoDB for the unit tests instead of the embedded one.
  I have following two dependencies in my POM.</p>
  
  <pre><code>    &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;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;
  </code></pre>
  
  <p>my properties file has the following:</p>
  
  <pre><code># MongoDB properties
  mongo.db.name=person_testDB
  mongo.db.url=localhost
  
  #external Mongo url
  spring.data.mongodb.uri=mongodb://localhost:27017/personDB
  </code></pre>
  
  <p>I have a config file(MongoDBConfig.java) which includes embedded MongoDB configurations:</p>
  
  <pre><code>@EnableMongoRepositories
  public class MongoDBConfig {
  
  @Value("${mongo.db.url}")
  private String MONGO_DB_URL;
  
  @Value("${mongo.db.name}")
  private String MONGO_DB_NAME;
  
  @Bean
  public MongoTemplate mongoTemplate() {
      MongoClient mongoClient = new MongoClient(MONGO_DB_URL);
      MongoTemplate mongoTemplate = new MongoTemplate(mongoClient, MONGO_DB_NAME);
      return mongoTemplate;
   }
  }
  </code></pre>
  
  <p>Following is my <code>PersonService.java</code> class:</p>
  
  <pre><code>@Service
  public class PersonService  {
  private static final Logger logger = LoggerFactory.getLogger(PersonService.class);      
  @Autowired
  MongoTemplate mongoTemplate;            
  public void savePerson(Person person) {
      String name = person.getName();
      String collectionName = name.substring(0, 1);
          mongoTemplate.save(person, collectionName);
      }               
  }
  </code></pre>
  
  <p>My unit test for the PersonsService class is as follows:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(classes = {MongoDBConfig.class})
  @SpringBootTest(classes = PersonService.class)
  @DataMongoTest
  public class PersonServiceTest {
  
  @Autowired
  PersonService personService;    
  MongodForTestsFactory factory;
  MongoClient mongo;
  
  @Before
  public void setup() throws Exception {
      factory = MongodForTestsFactory.with(Version.Main.PRODUCTION);
      mongo = factory.newMongo();
  }
  
  @After
  public void teardown() throws Exception {
      if (factory != null)
          factory.shutdown();
  }
  
  @Test
  public void testSave(){
  Person person = new Person("Bob Smith " , 25);
  personService.savePerson(person);
  }
  }
  </code></pre>
  
  <p>It creates the collection name and the document name correctly in the external MongoDB which is not what I want. How can I restrict the unitTests to an embedded Mongo? </p>