test: Simplified `InMemoryWithMongoDbStorageIT`
authorKai Moritz <kai@juplo.de>
Thu, 22 Feb 2024 13:49:46 +0000 (14:49 +0100)
committerKai Moritz <kai@juplo.de>
Thu, 22 Feb 2024 15:08:07 +0000 (16:08 +0100)
* Switched to `@ServiceConnection` instead of hand-coded initializer.

src/test/java/de/juplo/kafka/chat/backend/InMemoryWithMongoDbStorageIT.java

index 7566f52..7055af5 100644 (file)
@@ -1,6 +1,5 @@
 package de.juplo.kafka.chat.backend;
 
-import de.juplo.kafka.chat.backend.InMemoryWithMongoDbStorageIT.DataSourceInitializer;
 import de.juplo.kafka.chat.backend.implementation.StorageStrategy;
 import de.juplo.kafka.chat.backend.storage.mongodb.ChatRoomRepository;
 import de.juplo.kafka.chat.backend.storage.mongodb.MessageRepository;
@@ -12,13 +11,11 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
 import org.springframework.boot.test.autoconfigure.data.mongo.AutoConfigureDataMongo;
 import org.springframework.boot.test.context.TestConfiguration;
-import org.springframework.context.ApplicationContextInitializer;
-import org.springframework.context.ConfigurableApplicationContext;
+import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
 import org.springframework.context.annotation.Bean;
-import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.junit.jupiter.SpringExtension;
-import org.springframework.test.context.support.TestPropertySourceUtils;
 import org.testcontainers.containers.GenericContainer;
+import org.testcontainers.containers.MongoDBContainer;
 import org.testcontainers.containers.output.Slf4jLogConsumer;
 import org.testcontainers.junit.jupiter.Container;
 import org.testcontainers.junit.jupiter.Testcontainers;
@@ -30,7 +27,6 @@ import java.time.Clock;
 @ExtendWith({SpringExtension.class})
 @EnableAutoConfiguration
 @AutoConfigureDataMongo
-@ContextConfiguration(initializers = DataSourceInitializer.class)
 @Slf4j
 public class InMemoryWithMongoDbStorageIT extends AbstractInMemoryStorageIT
 {
@@ -72,31 +68,15 @@ public class InMemoryWithMongoDbStorageIT extends AbstractInMemoryStorageIT
     }
   }
 
-  private static final int MONGODB_PORT = 27017;
-
   @Container
-  private static final GenericContainer CONTAINER =
-      new GenericContainer("mongo:6").withExposedPorts(MONGODB_PORT);
-
-  public static class DataSourceInitializer
-      implements ApplicationContextInitializer<ConfigurableApplicationContext>
-  {
-    @Override
-    public void initialize(ConfigurableApplicationContext applicationContext)
-    {
-      TestPropertySourceUtils.addInlinedPropertiesToEnvironment(
-          applicationContext,
-          "spring.data.mongodb.host=localhost",
-          "spring.data.mongodb.port=" + CONTAINER.getMappedPort(MONGODB_PORT),
-          "spring.data.mongodb.database=test");
-    }
-  }
+  @ServiceConnection
+  private static final GenericContainer MONGODB = new MongoDBContainer("mongo:6");
 
   @BeforeEach
   void setUpLogging()
   {
     Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(log);
-    CONTAINER.followOutput(logConsumer);
+    MONGODB.followOutput(logConsumer);
     chatRoomRepository.deleteAll();
     messageRepository.deleteAll();
   }