title: How to use embedded MongoDB with SpringBoot v2.6.2?
body: <p>I'm using Spring Boot v2.6.2 and Java v17 and trying to test my MongoConnection without having a MongoDBService running because it should be tested with embedded in-memory MongoDB on the build machine, no need to set up an extra MongoDB service there.
  For sure on the productive system, it should use a full MongoDB.</p>
  <p>I try to get this easy example running:</p>
  <pre><code>@DataMongoTest
  public class MongoTest
  {
      @Autowired
      private UserRepository userRepository;
  
      @AfterEach
      void cleanUpDatabase()
      {
          this.userRepository.deleteAll();
      }
  
      @Test
      void bootstrapTestDataWithMongoTemplate() {
          final var restaurant = new User( &quot;123&quot;, &quot;ABC&quot;, &quot;DEF&quot; );
          this.userRepository.insert( restaurant );
  
          final var found = this.userRepository.findByFirstName( &quot;ABC&quot; );
  
          System.out.println( found );
      }
  }
  </code></pre>
  <p><code>UserRepository</code> is an interface that extends <code>MongoRepository</code>. <code>User</code> itself is just a <code>DBEntity</code> with <code>@Document</code> annotation. If I let the SpringBoot application run, not in test mode, everything works fine, because MongoDB is running at the specified location. But for the test, I want to let it run as an in-memory DB.</p>
  <p>But Springboot wants to connect for the test.</p>
  
  <pre class="lang-none prettyprint-override"><code>    2022-01-20 08:31:57.489  INFO 3976 --- [           main] org.mongodb.driver.cluster               : Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms'}
  2022-01-20 08:31:57.521  INFO 3976 --- [localhost:27017] org.mongodb.driver.cluster               : Exception in monitor thread while connecting to server localhost:27017
  
  com.mongodb.MongoSocketOpenException: Exception opening socket
      at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70) ~[mongodb-driver-core-4.4.0.jar:na]
      at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:180) ~[mongodb-driver-core-4.4.0.jar:na]
      at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.lookupServerDescription(DefaultServerMonitor.java:188) ~[mongodb-driver-core-4.4.0.jar:na]
      at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:152) ~[mongodb-driver-core-4.4.0.jar:na]
      at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]
  Caused by: java.net.ConnectException: Connection refused: no further information
      at java.base/sun.nio.ch.Net.pollConnect(Native Method) ~[na:na]
      at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:672) ~[na:na]
      at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:542) ~[na:na]
      at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:597) ~[na:na]
      at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327) ~[na:na]
      at java.base/java.net.Socket.connect(Socket.java:633) ~[na:na]
      at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:107) ~[mongodb-driver-core-4.4.0.jar:na]
      at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:79) ~[mongodb-driver-core-4.4.0.jar:na]
      at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65) ~[mongodb-driver-core-4.4.0.jar:na]
      ... 4 common frames omitted
  </code></pre>
  <p>Any suggestions on how to do this? Is <code>@DataMongoTest</code> testing in the wrong way? I thought the integrated flapdoodle dependency would inject it automatically in the test case.</p>
  <p>The pom itself is also not that complex:</p>
  <pre class="lang-none prettyprint-override"><code>&lt;properties&gt;
          &lt;java.version&gt;17&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-actuator&lt;/artifactId&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;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-security&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-devtools&lt;/artifactId&gt;
              &lt;optional&gt;true&lt;/optional&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
              &lt;artifactId&gt;lombok&lt;/artifactId&gt;
              &lt;optional&gt;true&lt;/optional&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;org.springframework.security&lt;/groupId&gt;
              &lt;artifactId&gt;spring-security-test&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;io.springfox&lt;/groupId&gt;
              &lt;artifactId&gt;springfox-swagger2&lt;/artifactId&gt;
              &lt;version&gt;3.0.0&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.github.openjson&lt;/groupId&gt;
              &lt;artifactId&gt;openjson&lt;/artifactId&gt;
              &lt;version&gt;1.0.11&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.github.erosb&lt;/groupId&gt;
              &lt;artifactId&gt;everit-json-schema&lt;/artifactId&gt;
              &lt;version&gt;1.14.0&lt;/version&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  </code></pre>