title: Test @Transactional with Flapdoodle Embedded MongoDB in Spring Boot
body: <p>I want to ensure that @Transactional annotation works so I wrote a test which save and publish article - my kafka publisher is a mock which throws an exception on any call. I want to ensure MongoDB rolls back the persisted article.</p>
  
  <pre><code>@Test
  void testRollbackOnPublishFail() {
      when(producer.publishArticle(any())).thenThrow(IllegalStateException.class);
      ArticleDocument articleDocument = ArticleTestDataUtil.createArticleDocument();
      try {
          ArticleDocument publishedDocument = articleService.saveAndPublish(articleDocument);
      } catch (Exception e) {
          assertTrue(e instanceof IllegalStateException);
      }
      assertFalse(articleService.findById(articleDocument.getId()).isPresent());
  }
  </code></pre>
  
  <p>I am using flapdoodles embedded mongo db for integration tests</p>
  
  <pre><code> testCompile "de.flapdoodle.embed:de.flapdoodle.embed.mongo:2.2.0"
  </code></pre>
  
  <p>This tests fails because there is no transaction / replication on default.</p>
  
  <p>So activated transactions by creating MongoTransactionManager:</p>
  
  <pre><code>@Configuration
  public class MongoTransactionConfig {
  
      @Bean
      public MongoTransactionManager transactionManager(MongoDbFactory dbFactory) {
          return new MongoTransactionManager(dbFactory);
      }
  }
  </code></pre>
  
  <p>Now my test fails because was not able to start a Session in MongoClient </p>
  
  <pre><code>com.mongodb.MongoClientException: Sessions are not supported by the MongoDB cluster to which this client is connected
          at com.mongodb.MongoClient.startSession(MongoClient.java:560)
  </code></pre>
  
  <p>I also tried to create a custom IMongodConfig</p>
  
  <pre><code>@Bean(name = "customReplicaMongodConfig")
      public IMongodConfig mongodConfig(EmbeddedMongoProperties embeddedProperties) throws IOException {
          Storage storage = new Storage("/tmp", "rs0", 0);
          return new MongodConfigBuilder()
                  .shardServer(true)
                  .version(Version.V4_0_2)
                  .net(new Net(27117, Network.localhostIsIPv6()))
                  .replication(storage)
                  .cmdOptions(new MongoCmdOptionsBuilder().useNoJournal(false).build()).build();
      }
  </code></pre>
  
  <p>And initiate the replication:</p>
  
  <pre><code>@ConditionalOnBean(name = "customReplicaMongodConfig")
  @Configuration
  public class ReplicaConfig {
  
      @Inject
      private MongoClient mongoClient;
  
      @PostConstruct
      public void initiateReplicationSet() {
          mongoClient.getDatabase("admin").runCommand(new Document("replSetInitiate", new Document()));
      }
  }
  </code></pre>
  
  <p>But the replSetInitiate failed with an timeout.</p>
  
  <p>So my question is if it is possible to create a running replication set with embedded MongoDB to test transactional.</p>