title: Embedded mongodb problems with spring
body: <p>I am developing a spring application, and I am storing all the data in mLab, so mongoDB. When the application runs, it takes the information from here, but, when trying to test the controllers, services, and repositories, it does not connect, that's why I tried to include embedded mongoDB.
  My application properties is (with the data filled):</p>
  
  <pre><code>spring.data.mongodb.database=
  spring.data.mongodb.host=
  spring.data.mongodb.port=37445 
  spring.data.mongodb.username=
  spring.data.mongodb.password=
  </code></pre>
  
  <p>Then, I have a MongoConfig class that is supposed to be the one used by the tests:</p>
  
  <pre><code>import java.io.IOException;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.data.mongodb.core.MongoTemplate;
  
  import com.mongodb.MongoClient;
  
  import cz.jirutka.spring.embedmongo.EmbeddedMongoFactoryBean;
  
  @Configuration
  public class MongoConfig {
      private static final String MONGO_DB_URL = "localhost";
      private static final String MONGO_DB_NAME = "test";
  
      @Bean
      public MongoTemplate mongoTemplate() throws IOException {
          EmbeddedMongoFactoryBean mongo = new EmbeddedMongoFactoryBean();
          mongo.setBindIp(MONGO_DB_URL);
          MongoClient mongoClient = mongo.getObject();
          MongoTemplate mongoTemplate = new MongoTemplate(mongoClient, MONGO_DB_NAME);
          return mongoTemplate;
      }
  }
  </code></pre>
  
  <p>And the Controller test is:</p>
  
  <pre><code>import static org.junit.Assert.assertEquals;
  import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
  import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl;
  
  import org.junit.Before;
  import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.mockito.InjectMocks;
  import org.mockito.MockitoAnnotations;
  import org.springframework.boot.test.context.SpringBootTest;
  import org.springframework.http.HttpStatus;
  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  import org.springframework.test.web.servlet.MockMvc;
  import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
  import org.springframework.test.web.servlet.setup.MockMvcBuilders;
  
  import com.uniovi.Application;
  
  @SpringBootTest(classes = { Application.class })
  @RunWith(SpringJUnit4ClassRunner.class)
  public class OperatorControllerTest {
  
      @InjectMocks
      private OperatorControllerTest operatorController;
  
      private MockMvc mockMvc;
  
      @Before
      public void setUp() throws Exception {
          MockitoAnnotations.initMocks(this);
          this.mockMvc = MockMvcBuilders.standaloneSetup(operatorController).build();
      }
  
      @Test
      public void testNotLoggedIn() throws Exception {
          MockHttpServletRequestBuilder request = get("/operator/list");
          int status = mockMvc.perform(request).andExpect(redirectedUrl("/login")).andReturn().getResponse().getStatus();
          assertEquals(HttpStatus.FOUND.value(), status);
      }
  
  }
  </code></pre>
  
  <p>The pom.xml has these three dependencies(apart from the others for the rest of the project):</p>
  
  <pre><code>    &lt;dependency&gt;
              &lt;groupId&gt;de.flapdoodle.embed&lt;/groupId&gt;
              &lt;artifactId&gt;de.flapdoodle.embed.mongo&lt;/artifactId&gt;
              &lt;version&gt;2.0.0&lt;/version&gt;&lt;!--$NO-MVN-MAN-VER$--&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;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;
  </code></pre>
  
  <p>The problem is that, when executing, I get this error:</p>
  
  <blockquote>
    <p>java.lang.IllegalStateException: Failed to load ApplicationContext    at
    org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125)
    ~[spring-test-5.0.4.RELEASE.jar:5.0.4.RELEASE]    at
    org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:107)
    ~[spring-test-5.0.4.RELEASE.jar:5.0.4.RELEASE]    at
    org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:190)
    ~[spring-test-5.0.4.RELEASE.jar:5.0.4.RELEASE]    at
    org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:132)
    ~[spring-test-5.0.4.RELEASE.jar:5.0.4.RELEASE]    at
    org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:242)
    ~[spring-test-5.0.4.RELEASE.jar:5.0.4.RELEASE]    at
    org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227) [spring-test-5.0.4.RELEASE.jar:5.0.4.RELEASE]    at
    org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
    [spring-test-5.0.4.RELEASE.jar:5.0.4.RELEASE]     at
    org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    [junit-4.12.jar:4.12]     at
    org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
    [spring-test-5.0.4.RELEASE.jar:5.0.4.RELEASE]     at
    org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246)
    [spring-test-5.0.4.RELEASE.jar:5.0.4.RELEASE]     at
    org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
    [spring-test-5.0.4.RELEASE.jar:5.0.4.RELEASE]     at
    org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    [junit-4.12.jar:4.12]     at
    org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    [junit-4.12.jar:4.12]     at
    org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    [junit-4.12.jar:4.12]     at
    org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    [junit-4.12.jar:4.12]     at
    org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    [junit-4.12.jar:4.12]     at
    org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    [spring-test-5.0.4.RELEASE.jar:5.0.4.RELEASE]     at
    org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    [spring-test-5.0.4.RELEASE.jar:5.0.4.RELEASE]     at
    org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    [junit-4.12.jar:4.12]     at
    org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
    [spring-test-5.0.4.RELEASE.jar:5.0.4.RELEASE]     at
    org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    [.cp/:na]     at
    org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    [.cp/:na]     at
    org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    [.cp/:na]     at
    org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    [.cp/:na]     at
    org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    [.cp/:na]     at
    org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
    [.cp/:na] Caused by:
    org.springframework.beans.factory.BeanCreationException: Error
    creating bean with name 'embeddedMongoServer' defined in class path
    resource
    [org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.class]:
    Invocation of init method failed; nested exception is
    java.io.IOException: Could not start process: failed errno:10049 La
    dirección solicitada no es válida en este contexto. for socket:
    34.243.17.210:37445 2018-04-11T19:56:30.505+0200 E STORAGE  [initandlisten] Failed to set up sockets during startup.
    2018-04-11T19:56:30.505+0200 I CONTROL  [initandlisten] dbexit:  rc:
    48</p>
    
    <p>at
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1710)
    ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]   at
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:583)
    ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]   at
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502)
    ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]   at
    org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312)
    ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]   at
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]   at
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)
    ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]   at
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
    ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]   at
    org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:760)
    ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]   at
    org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868)
    ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]     at
    org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
    ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]     at
    org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752)
    ~[spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]    at
    org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:388)
    ~[spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]    at
    org.springframework.boot.SpringApplication.run(SpringApplication.java:327)
    ~[spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]    at
    org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:138)
    ~[spring-boot-test-2.0.0.RELEASE.jar:2.0.0.RELEASE]   at
    org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)
    ~[spring-test-5.0.4.RELEASE.jar:5.0.4.RELEASE]    at
    org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:117)
    ~[spring-test-5.0.4.RELEASE.jar:5.0.4.RELEASE]    ... 25 common frames
    omitted Caused by: java.io.IOException: Could not start process:
    failed errno:10049 La dirección solicitada no es válida en este
    contexto. for socket: 34.243.17.210:37445 2018-04-11T19:56:30.505+0200
    E STORAGE  [initandlisten] Failed to set up sockets during startup.
    2018-04-11T19:56:30.505+0200 I CONTROL  [initandlisten] dbexit:  rc:
    48</p>
    
    <p>at
    de.flapdoodle.embed.mongo.AbstractMongoProcess.onAfterProcessStart(AbstractMongoProcess.java:79)
    ~[de.flapdoodle.embed.mongo-2.0.0.jar:na]     at
    de.flapdoodle.embed.process.runtime.AbstractProcess.(AbstractProcess.java:114)
    ~[de.flapdoodle.embed.process-2.0.1.jar:na]   at
    de.flapdoodle.embed.mongo.AbstractMongoProcess.(AbstractMongoProcess.java:53)
    ~[de.flapdoodle.embed.mongo-2.0.0.jar:na]     at
    de.flapdoodle.embed.mongo.MongodProcess.(MongodProcess.java:50)
    ~[de.flapdoodle.embed.mongo-2.0.0.jar:na]     at
    de.flapdoodle.embed.mongo.MongodExecutable.start(MongodExecutable.java:44)
    ~[de.flapdoodle.embed.mongo-2.0.0.jar:na]     at
    de.flapdoodle.embed.mongo.MongodExecutable.start(MongodExecutable.java:34)
    ~[de.flapdoodle.embed.mongo-2.0.0.jar:na]     at
    de.flapdoodle.embed.process.runtime.Executable.start(Executable.java:101)
    ~[de.flapdoodle.embed.process-2.0.1.jar:na]   at
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    ~[na:1.8.0_161]   at
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    ~[na:1.8.0_161]   at
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    ~[na:1.8.0_161]   at java.lang.reflect.Method.invoke(Method.java:498)
    ~[na:1.8.0_161]   at
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1835)
    ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]   at
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1778)
    ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]   at
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1706)
    ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]   ... 40 common frames
    omitted</p>
  </blockquote>
  
  <p>I have already looked all over the internet, and all the different solutions are useless for my code.</p>