From: Kai Moritz Date: Thu, 22 Feb 2024 13:49:46 +0000 (+0100) Subject: test: Simplified `InMemoryWithMongoDbStorageIT` X-Git-Tag: rebase--2024-02-23--16-28~23 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=b4ba2e35e606a6463e9842a15c9f77b97435ecb5;p=demos%2Fkafka%2Fchat test: Simplified `InMemoryWithMongoDbStorageIT` * Switched to `@ServiceConnection` instead of hand-coded initializer. --- diff --git a/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithMongoDbStorageIT.java b/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithMongoDbStorageIT.java index 7566f521..7055af54 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithMongoDbStorageIT.java +++ b/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithMongoDbStorageIT.java @@ -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 - { - @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(); }