diff --git a/consumer/mongodb-consumer/pom.xml b/consumer/mongodb-consumer/pom.xml index 0f2a26d9..fe1ef617 100644 --- a/consumer/mongodb-consumer/pom.xml +++ b/consumer/mongodb-consumer/pom.xml @@ -24,8 +24,8 @@ mongodb-driver-reactivestreams - de.flapdoodle.embed - de.flapdoodle.embed.mongo + org.testcontainers + mongodb test diff --git a/consumer/mongodb-consumer/src/test/java/org/springframework/cloud/fn/consumer/mongo/MongoDbConsumerApplicationTests.java b/consumer/mongodb-consumer/src/test/java/org/springframework/cloud/fn/consumer/mongo/MongoDbConsumerApplicationTests.java index c9ba7272..4f633e0a 100644 --- a/consumer/mongodb-consumer/src/test/java/org/springframework/cloud/fn/consumer/mongo/MongoDbConsumerApplicationTests.java +++ b/consumer/mongodb-consumer/src/test/java/org/springframework/cloud/fn/consumer/mongo/MongoDbConsumerApplicationTests.java @@ -33,17 +33,26 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.mongodb.core.ReactiveMongoTemplate; import org.springframework.messaging.Message; import org.springframework.messaging.support.GenericMessage; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; import static org.assertj.core.api.Assertions.assertThat; import static org.awaitility.Awaitility.await; /** * @author David Turanski + * @author Chris Bono */ @SpringBootTest(properties = { - "mongodb.consumer.collection=testing", - "spring.mongodb.embedded.version=3.5.5" }) -class MongoDbConsumerApplicationTests { + "mongodb.consumer.collection=testing" +}) +class MongoDbConsumerApplicationTests implements MongoDbTestContainerSupport { + + @DynamicPropertySource + static void mongoDbProperties(DynamicPropertyRegistry registry) { + registry.add("spring.data.mongodb.uri", MongoDbTestContainerSupport::mongoDbUri); + registry.add("spring.data.mongodb.database", () -> "test"); + } @Autowired private MongoDbConsumerProperties properties; diff --git a/consumer/mongodb-consumer/src/test/java/org/springframework/cloud/fn/consumer/mongo/MongoDbTestContainerSupport.java b/consumer/mongodb-consumer/src/test/java/org/springframework/cloud/fn/consumer/mongo/MongoDbTestContainerSupport.java new file mode 100644 index 00000000..5c0a8570 --- /dev/null +++ b/consumer/mongodb-consumer/src/test/java/org/springframework/cloud/fn/consumer/mongo/MongoDbTestContainerSupport.java @@ -0,0 +1,42 @@ +/* + * Copyright 2022-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.fn.consumer.mongo; + +import org.junit.jupiter.api.BeforeAll; +import org.testcontainers.containers.MongoDBContainer; +import org.testcontainers.junit.jupiter.Testcontainers; + +/** + * Provides a static {@link MongoDBContainer} that can be shared across test classes. + * + * @author Chris Bono + */ +@Testcontainers(disabledWithoutDocker = true) +public interface MongoDbTestContainerSupport { + + MongoDBContainer MONGO_CONTAINER = new MongoDBContainer("mongo:5.0.9"); + + @BeforeAll + static void startContainer() { + MONGO_CONTAINER.start(); + } + + static String mongoDbUri() { + return "mongodb://localhost:" + MONGO_CONTAINER.getFirstMappedPort(); + } + +} diff --git a/consumer/mongodb-consumer/src/test/resources/logback-test.xml b/consumer/mongodb-consumer/src/test/resources/logback-test.xml new file mode 100644 index 00000000..1378a823 --- /dev/null +++ b/consumer/mongodb-consumer/src/test/resources/logback-test.xml @@ -0,0 +1,14 @@ + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n + + + + + + + + + +