[camel] branch master updated: CAMEL-14566: camel-mongodb - Use testcontainers for testing (#3580)

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[camel] branch master updated: CAMEL-14566: camel-mongodb - Use testcontainers for testing (#3580)

davsclaus-2
This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new 945d066  CAMEL-14566: camel-mongodb - Use testcontainers for testing (#3580)
945d066 is described below

commit 945d0666e22a983a2ce2c23bf6c2626cf3753969
Author: Luca Burgazzoli <[hidden email]>
AuthorDate: Sat Feb 15 08:53:14 2020 +0100

    CAMEL-14566: camel-mongodb - Use testcontainers for testing (#3580)
   
    * CAMEL-14566: camel-mongodb - Use testcontainers for testing
   
    * camel-mongodb: replace deprecated methods and small code cleanup
---
 components/camel-mongodb/pom.xml                   | 88 ++++++++++++++++------
 .../camel/component/mongodb/MongoDbProducer.java   | 38 +++++-----
 .../converters/MongoDbFallbackConverter.java       |  7 +-
 .../idempotent/MongoDbIdempotentRepository.java    |  2 +-
 .../component/mongodb/AbstractMongoDbTest.java     | 39 +++++++---
 .../component/mongodb/EmbedMongoConfiguration.java | 72 ------------------
 .../mongodb/MongoBasicOperationsConfiguration.java | 27 -------
 .../mongodb/MongoDbConnectionBeansTest.java        | 57 ++++++++------
 .../camel/component/mongodb/MongoDbContainer.java  | 86 +++++++++++++++++++++
 .../component/mongodb/MongoDbOperationsTest.java   |  4 +-
 .../mongodb/MongoDbSpringDslOperationsTest.java    | 15 +++-
 .../mongodb/meta/MongoDbMetaExtensionTest.java     |  8 +-
 .../verifier/MongoDbVerifierExtensionTest.java     | 14 +---
 .../src/test/resources/log4j2.properties           |  6 +-
 .../src/test/resources/mongodb.test.properties     |  2 +-
 15 files changed, 261 insertions(+), 204 deletions(-)

diff --git a/components/camel-mongodb/pom.xml b/components/camel-mongodb/pom.xml
index 1633a79..65075f0 100644
--- a/components/camel-mongodb/pom.xml
+++ b/components/camel-mongodb/pom.xml
@@ -57,10 +57,17 @@
         <!-- test dependencies -->
         <dependency>
             <groupId>org.apache.camel</groupId>
-            <artifactId>camel-test-spring-junit5</artifactId>
+            <artifactId>camel-testcontainers-spring-junit5</artifactId>
             <scope>test</scope>
         </dependency>
         <dependency>
+            <groupId>org.apache.commons</groupId>
+            <artifactId>commons-lang3</artifactId>
+            <version>${commons-lang3-version}</version>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
             <groupId>org.apache.logging.log4j</groupId>
             <artifactId>log4j-api</artifactId>
             <scope>test</scope>
@@ -75,37 +82,76 @@
             <artifactId>log4j-slf4j-impl</artifactId>
             <scope>test</scope>
         </dependency>
-        <dependency>
-            <groupId>de.flapdoodle.embed</groupId>
-            <artifactId>de.flapdoodle.embed.mongo</artifactId>
-            <scope>test</scope>
-        </dependency>
     </dependencies>
 
-    <!-- skip tests on AIX and HP-UX -->
+
+
     <profiles>
         <profile>
-            <id>aix</id>
+            <id>etcd-skip-tests</id>
+            <activation>
+                <activeByDefault>true</activeByDefault>
+            </activation>
+            <build>
+                <plugins>
+                    <plugin>
+                        <artifactId>maven-surefire-plugin</artifactId>
+                        <configuration>
+                            <skipTests>true</skipTests>
+                        </configuration>
+                    </plugin>
+                </plugins>
+            </build>
+        </profile>
+
+        <!-- activate test if the docker socket file is accessible -->
+        <profile>
+            <id>etcd-tests-docker-file</id>
             <activation>
-                <os>
-                    <family>AIX</family>
-                </os>
+                <file>
+                    <exists>/var/run/docker.sock</exists>
+                </file>
             </activation>
-            <properties>
-                <skipTests>true</skipTests>
-            </properties>
+            <build>
+                <plugins>
+                    <plugin>
+                        <artifactId>maven-surefire-plugin</artifactId>
+                        <configuration>
+                            <skipTests>${skipTests}</skipTests>
+                            <forkCount>1</forkCount>
+                            <systemPropertyVariables>
+                                <visibleassertions.silence>true</visibleassertions.silence>
+                            </systemPropertyVariables>
+                        </configuration>
+                    </plugin>
+                </plugins>
+            </build>
         </profile>
+
+        <!-- activate test if the DOCKER_HOST env var is set -->
         <profile>
-            <id>hpux</id>
+            <id>etcd-tests-docker-env</id>
             <activation>
-                <os>
-                    <family>HP-UX</family>
-                </os>
+                <property>
+                    <name>env.DOCKER_HOST</name>
+                </property>
             </activation>
-            <properties>
-                <skipTests>true</skipTests>
-            </properties>
+            <build>
+                <plugins>
+                    <plugin>
+                        <artifactId>maven-surefire-plugin</artifactId>
+                        <configuration>
+                            <skipTests>${skipTests}</skipTests>
+                            <forkCount>1</forkCount>
+                            <systemPropertyVariables>
+                                <visibleassertions.silence>true</visibleassertions.silence>
+                            </systemPropertyVariables>
+                        </configuration>
+                    </plugin>
+                </plugins>
+            </build>
         </profile>
+
     </profiles>
 
 </project>
diff --git a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbProducer.java b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbProducer.java
index 760d60e..e7b3eaa 100644
--- a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbProducer.java
+++ b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbProducer.java
@@ -17,14 +17,13 @@
 package org.apache.camel.component.mongodb;
 
 import java.util.ArrayList;
-import java.util.Arrays;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.function.Function;
 import java.util.stream.Collectors;
 
-import com.mongodb.bulk.BulkWriteResult;
 import com.mongodb.client.AggregateIterable;
 import com.mongodb.client.DistinctIterable;
 import com.mongodb.client.FindIterable;
@@ -32,6 +31,7 @@ import com.mongodb.client.MongoCollection;
 import com.mongodb.client.MongoDatabase;
 import com.mongodb.client.model.BulkWriteOptions;
 import com.mongodb.client.model.Filters;
+import com.mongodb.client.model.ReplaceOptions;
 import com.mongodb.client.model.UpdateOptions;
 import com.mongodb.client.model.WriteModel;
 import com.mongodb.client.result.DeleteResult;
@@ -48,7 +48,6 @@ import org.bson.conversions.Bson;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-
 import static com.mongodb.client.model.Filters.eq;
 import static org.apache.camel.component.mongodb.MongoDbConstants.BATCH_SIZE;
 import static org.apache.camel.component.mongodb.MongoDbConstants.COLLECTION;
@@ -330,7 +329,7 @@ public class MongoDbProducer extends DefaultProducer {
 
     private Function<Exchange, Object> createDoDistinct() {
         return exchange -> {
-            Iterable<String> result = new ArrayList<>();
+            List<String> result = new ArrayList<>();
             MongoCollection<Document> dbCol = calculateCollection(exchange);
 
             // get the parameters out of the Exchange Header
@@ -344,8 +343,8 @@ public class MongoDbProducer extends DefaultProducer {
             }
 
             try {
-                ret.iterator().forEachRemaining(((List<String>) result)::add);
-                exchange.getMessage().setHeader(MongoDbConstants.RESULT_PAGE_SIZE, ((List<String>) result).size());
+                ret.iterator().forEachRemaining(result::add);
+                exchange.getMessage().setHeader(MongoDbConstants.RESULT_PAGE_SIZE, result.size());
             } finally {
                 ret.iterator().close();
             }
@@ -484,9 +483,8 @@ public class MongoDbProducer extends DefaultProducer {
                 } else {
                     result = dbCol.updateMany(updateCriteria, objNew, options);
                 }
-                if (result.isModifiedCountAvailable()) {
-                    exchange.getMessage().setHeader(RECORDS_AFFECTED, result.getModifiedCount());
-                }
+
+                exchange.getMessage().setHeader(RECORDS_AFFECTED, result.getModifiedCount());
                 exchange.getMessage().setHeader(RECORDS_MATCHED, result.getMatchedCount());
                 return result;
             } catch (InvalidPayloadException e) {
@@ -518,22 +516,22 @@ public class MongoDbProducer extends DefaultProducer {
                 MongoCollection<Document> dbCol = calculateCollection(exchange);
                 @SuppressWarnings("unchecked")
                 List<Bson> query = exchange.getIn().getMandatoryBody((Class<List<Bson>>)Class.class.cast(List.class));
-                
+
                 // Allow body to be a pipeline
                 // @see http://docs.mongodb.org/manual/core/aggregation/
                 List<Bson> queryList;
                 if (query != null) {
-                    queryList = query.stream().collect(Collectors.toList());
+                    queryList = new ArrayList<>(query);
                 } else {
-                    queryList = Arrays.asList(Bson.class.cast(exchange.getIn().getMandatoryBody(Bson.class)));
+                    queryList = Collections.singletonList(exchange.getIn().getMandatoryBody(Bson.class));
                 }
-                
+
                 // The number to skip must be in body query
                 AggregateIterable<Document> aggregationResult = dbCol.aggregate(queryList);
-                
+
                 // get the batch size
                 Integer batchSize = exchange.getIn().getHeader(MongoDbConstants.BATCH_SIZE, Integer.class);
-                
+
                 if (batchSize != null) {
                     aggregationResult.batchSize(batchSize);
                 }
@@ -553,7 +551,7 @@ public class MongoDbProducer extends DefaultProducer {
                 } else {
                     result = aggregationResult;
                 }
-                
+
                 return result;
             } catch (InvalidPayloadException e) {
                 throw new CamelMongoDbException("Invalid payload for aggregate", e);
@@ -603,7 +601,7 @@ public class MongoDbProducer extends DefaultProducer {
             try {
                 MongoCollection<Document> dbCol = calculateCollection(exchange);
                 Document saveObj = exchange.getIn().getMandatoryBody(Document.class);
-                UpdateOptions options = new UpdateOptions().upsert(true);
+                ReplaceOptions options = new ReplaceOptions().upsert(true);
                 UpdateResult result;
                 if (null == saveObj.get(MONGO_ID)) {
                     result = dbCol.replaceOne(Filters.where("false"), saveObj, options);
@@ -618,7 +616,7 @@ public class MongoDbProducer extends DefaultProducer {
             }
         };
     }
-    
+
     private Function<Exchange, Object> createDoBulkWrite() {
         return exchange -> {
             try {
@@ -630,9 +628,7 @@ public class MongoDbProducer extends DefaultProducer {
                 @SuppressWarnings("unchecked")
                 List<WriteModel<Document>> requests = exchange.getIn().getMandatoryBody((Class<List<WriteModel<Document>>>)Class.class.cast(List.class));
 
-                BulkWriteResult result = dbCol.bulkWrite(requests, options);
-                return result;
-
+                return dbCol.bulkWrite(requests, options);
             } catch (InvalidPayloadException e) {
                 throw new CamelMongoDbException("Invalid payload for bulk write", e);
             }
diff --git a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/converters/MongoDbFallbackConverter.java b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/converters/MongoDbFallbackConverter.java
index 88ecea5..b513f52 100644
--- a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/converters/MongoDbFallbackConverter.java
+++ b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/converters/MongoDbFallbackConverter.java
@@ -23,7 +23,6 @@ import com.fasterxml.jackson.databind.ObjectMapper;
 import com.mongodb.BasicDBList;
 import com.mongodb.BasicDBObject;
 import com.mongodb.DBObject;
-import com.mongodb.util.JSON;
 import org.apache.camel.Converter;
 import org.apache.camel.Exchange;
 import org.apache.camel.InvalidPayloadException;
@@ -48,21 +47,21 @@ public final class MongoDbFallbackConverter {
         if (String.class == value.getClass()) {
 
             if (type == DBObject.class) {
-                Object out = JSON.parse(value.toString());
+                Object out = BasicDBObject.parse(value.toString());
                 if (out instanceof DBObject) {
                     return out;
                 } else {
                     throw new InvalidPayloadException(exchange, type);
                 }
             } else if (type == BasicDBList.class) {
-                Object out = JSON.parse(value.toString());
+                Object out = BasicDBObject.parse(value.toString());
                 if (out instanceof BasicDBList) {
                     return out;
                 } else {
                     throw new InvalidPayloadException(exchange, type);
                 }
             } else if (type == BasicDBObject.class) {
-                Object out = JSON.parse(value.toString());
+                Object out = BasicDBObject.parse(value.toString());
                 if (out instanceof BasicDBObject) {
                     return out;
                 } else {
diff --git a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/processor/idempotent/MongoDbIdempotentRepository.java b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/processor/idempotent/MongoDbIdempotentRepository.java
index 8c6d8aa..9e450aa 100644
--- a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/processor/idempotent/MongoDbIdempotentRepository.java
+++ b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/processor/idempotent/MongoDbIdempotentRepository.java
@@ -67,7 +67,7 @@ public class MongoDbIdempotentRepository extends ServiceSupport implements Idemp
     @Override
     public boolean contains(String key) {
         Bson document = eq(MONGO_ID, key);
-        long count = collection.count(document);
+        long count = collection.countDocuments(document);
         return count > 0;
     }
 
diff --git a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/AbstractMongoDbTest.java b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/AbstractMongoDbTest.java
index 1a60901..76b4cbe 100644
--- a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/AbstractMongoDbTest.java
+++ b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/AbstractMongoDbTest.java
@@ -23,14 +23,14 @@ import com.mongodb.client.MongoCollection;
 import com.mongodb.client.MongoDatabase;
 import org.apache.camel.CamelContext;
 import org.apache.camel.CamelExecutionException;
-import org.apache.camel.spring.SpringCamelContext;
+import org.apache.camel.impl.DefaultCamelContext;
 import org.apache.camel.test.junit5.CamelTestSupport;
 import org.apache.camel.util.IOHelper;
 import org.apache.camel.util.ObjectHelper;
 import org.bson.Document;
+import org.junit.jupiter.api.AfterAll;
 import org.junit.jupiter.api.AfterEach;
-import org.springframework.context.ApplicationContext;
-import org.springframework.context.annotation.AnnotationConfigApplicationContext;
+import org.junit.jupiter.api.BeforeAll;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -38,10 +38,10 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
 public abstract class AbstractMongoDbTest extends CamelTestSupport {
 
     protected static final String SCHEME = "mongodb";
-    protected static final String HOST = "localhost:" + EmbedMongoConfiguration.PORT;
     protected static final String USER = "test-user";
     protected static final String PASSWORD = "test-pwd";
 
+    protected static MongoDbContainer container;
     protected static MongoClient mongo;
     protected static MongoDatabase db;
     protected static MongoCollection<Document> testCollection;
@@ -51,13 +51,29 @@ public abstract class AbstractMongoDbTest extends CamelTestSupport {
     protected static String testCollectionName;
     protected static String dynamicCollectionName;
 
-    protected ApplicationContext applicationContext;
+    @BeforeAll
+    public static void doBeforeAll() {
+        container = new MongoDbContainer();
+        container.start();
+    }
+
+    @AfterAll
+    public static void doAfterAll() {
+        if (container != null) {
+            container.stop();
+        }
+    }
 
     @Override
-    public void doPostSetup() {
-        mongo = applicationContext.getBean("myDb", MongoClient.class);
+    public void doPreSetup() throws Exception {
+        super.doPreSetup();
+
+        mongo = container.createClient();
         db = mongo.getDatabase(dbName);
+    }
 
+    @Override
+    public void doPostSetup() {
         // Refresh the test collection - drop it and recreate it. We don't do
         // this for the database because MongoDB would create large
         // store files each time
@@ -70,7 +86,6 @@ public abstract class AbstractMongoDbTest extends CamelTestSupport {
         dynamicCollection = db.getCollection(dynamicCollectionName, Document.class);
         dynamicCollection.drop();
         dynamicCollection = db.getCollection(dynamicCollectionName, Document.class);
-
     }
 
     @Override
@@ -84,10 +99,14 @@ public abstract class AbstractMongoDbTest extends CamelTestSupport {
 
     @Override
     protected CamelContext createCamelContext() throws Exception {
-        applicationContext = new AnnotationConfigApplicationContext(EmbedMongoConfiguration.class);
+        MongoDbComponent component = new MongoDbComponent();
+        component.setMongoConnection(mongo);
+
         @SuppressWarnings("deprecation")
-        CamelContext ctx = SpringCamelContext.springCamelContext(applicationContext, true);
+        CamelContext ctx = new DefaultCamelContext();
         ctx.getPropertiesComponent().setLocation("classpath:mongodb.test.properties");
+        ctx.addComponent(SCHEME, component);
+
         return ctx;
     }
 
diff --git a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/EmbedMongoConfiguration.java b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/EmbedMongoConfiguration.java
deleted file mode 100644
index 9ed2e3c..0000000
--- a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/EmbedMongoConfiguration.java
+++ /dev/null
@@ -1,72 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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
- *
- *      http://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.apache.camel.component.mongodb;
-
-import java.io.IOException;
-import java.net.UnknownHostException;
-
-import com.mongodb.client.MongoClient;
-import com.mongodb.client.MongoClients;
-import de.flapdoodle.embed.mongo.MongodExecutable;
-import de.flapdoodle.embed.mongo.MongodStarter;
-import de.flapdoodle.embed.mongo.config.IMongodConfig;
-import de.flapdoodle.embed.mongo.config.MongodConfigBuilder;
-import de.flapdoodle.embed.mongo.config.Net;
-import de.flapdoodle.embed.mongo.config.Storage;
-import org.bson.Document;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-
-import static de.flapdoodle.embed.mongo.distribution.Version.Main.PRODUCTION;
-import static de.flapdoodle.embed.process.runtime.Network.localhostIsIPv6;
-import static org.springframework.util.SocketUtils.findAvailableTcpPort;
-
-@Configuration
-public class EmbedMongoConfiguration {
-
-    public static final int PORT = findAvailableTcpPort();
-
-    static {
-        try {
-            IMongodConfig mongodConfig = new MongodConfigBuilder()
-                    .version(PRODUCTION)
-                    .net(new Net(PORT, localhostIsIPv6()))
-                    .replication(new Storage(null, "replicationName", 5000))
-                    .build();
-
-            MongodExecutable mongodExecutable = MongodStarter.getDefaultInstance().prepare(mongodConfig);
-            mongodExecutable.start();
-
-            // init replica set
-            MongoClient client = MongoClients.create("mongodb://localhost:" + PORT);
-            client.getDatabase("admin").runCommand(new Document("replSetInitiate", new Document()));
-
-        } catch (IOException e) {
-            throw new RuntimeException(e);
-        }
-    }
-
-    @Bean
-    public MongoClient myDb() throws UnknownHostException {
-        return MongoClients.create("mongodb://localhost:" + PORT);
-    }
-
-    @Bean
-    public MongoClient myDbS() throws UnknownHostException {
-        return MongoClients.create("mongodb://localhost:" + PORT);
-    }
-}
diff --git a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoBasicOperationsConfiguration.java b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoBasicOperationsConfiguration.java
deleted file mode 100644
index 40499ea..0000000
--- a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoBasicOperationsConfiguration.java
+++ /dev/null
@@ -1,27 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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
- *
- *      http://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.apache.camel.component.mongodb;
-
-import org.springframework.context.annotation.Configuration;
-import org.springframework.context.annotation.Import;
-import org.springframework.context.annotation.ImportResource;
-
-@Configuration
-@Import(EmbedMongoConfiguration.class)
-@ImportResource("org/apache/camel/component/mongodb/mongoBasicOperationsTest.xml")
-public class MongoBasicOperationsConfiguration {
-}
diff --git a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbConnectionBeansTest.java b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbConnectionBeansTest.java
index 60e9368..b902b63 100644
--- a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbConnectionBeansTest.java
+++ b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbConnectionBeansTest.java
@@ -20,58 +20,65 @@ import com.mongodb.client.MongoClient;
 import org.apache.camel.Endpoint;
 import org.junit.jupiter.api.Test;
 
+import static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNotEquals;
 import static org.junit.jupiter.api.Assertions.assertThrows;
-import static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf;
 
 public class MongoDbConnectionBeansTest extends AbstractMongoDbTest {
-
     @Test
     public void checkConnectionFromProperties() {
-        MongoDbEndpoint testEndpoint = context.getEndpoint(
-                "mongodb:anyName?mongoConnection=#myDb&database={{mongodb.testDb}}&collection={{mongodb.testCollection}}&operation=count&dynamicity=true",
-                MongoDbEndpoint.class);
+        MongoClient client = container.createClient();
+
+        context.getComponent(SCHEME, MongoDbComponent.class).setMongoConnection(null);
+        context.getRegistry().bind("myDb", client);
+
+        MongoDbEndpoint testEndpoint = context.getEndpoint("mongodb:anyName?mongoConnection=#myDb", MongoDbEndpoint.class);
+
         assertNotEquals("myDb", testEndpoint.getConnectionBean());
-        assertEquals(mongo, testEndpoint.getMongoConnection());
+        assertEquals(client, testEndpoint.getMongoConnection());
     }
 
     @Test
     public void checkConnectionFromBean() {
-        MongoDbEndpoint testEndpoint = context.getEndpoint(
-                "mongodb:myDb?database={{mongodb.testDb}}&collection={{mongodb.testCollection}}&operation=count&dynamicity=true",
-                MongoDbEndpoint.class);
+        MongoClient client = container.createClient();
+
+        context.getComponent(SCHEME, MongoDbComponent.class).setMongoConnection(null);
+        context.getRegistry().bind("myDb", client);
+
+        MongoDbEndpoint testEndpoint = context.getEndpoint("mongodb:myDb", MongoDbEndpoint.class);
         assertEquals("myDb", testEndpoint.getConnectionBean());
-        assertEquals(mongo, testEndpoint.getMongoConnection());
+        assertEquals(client, testEndpoint.getMongoConnection());
     }
 
+
     @Test
     public void checkConnectionBothExisting() {
-        MongoDbEndpoint testEndpoint = context.getEndpoint(
-                "mongodb:myDb?mongoConnection=#myDbS&database={{mongodb.testDb}}&collection={{mongodb.testCollection}}&operation=count&dynamicity=true",
-                MongoDbEndpoint.class);
+        MongoClient client1 = container.createClient();
+        MongoClient client2 = container.createClient();
+
+        context.getComponent(SCHEME, MongoDbComponent.class).setMongoConnection(null);
+        context.getRegistry().bind("myDb", client1);
+        context.getRegistry().bind("myDbS", client2);
+
+        MongoDbEndpoint testEndpoint = context.getEndpoint("mongodb:myDb?mongoConnection=#myDbS", MongoDbEndpoint.class);
+        MongoClient myDbS = context.getRegistry().lookupByNameAndType("myDbS", MongoClient.class);
+
         assertEquals("myDb", testEndpoint.getConnectionBean());
-        MongoClient myDbS = applicationContext.getBean("myDbS", MongoClient.class);
         assertEquals(myDbS, testEndpoint.getMongoConnection());
     }
 
     @Test
     public void checkMissingConnection() {
-        assertThrows(Exception.class, () -> {
-            MongoDbEndpoint testEndpoint = context
-                .getEndpoint("mongodb:anythingNotRelated?database={{mongodb.testDb}}&collection={{mongodb.testCollection}}&operation=count&dynamicity=true", MongoDbEndpoint.class);
-        });
+        context.getComponent(SCHEME, MongoDbComponent.class).setMongoConnection(null);
+        assertThrows(Exception.class, () -> context.getEndpoint("mongodb:anythingNotRelated", MongoDbEndpoint.class));
     }
 
     @Test
     public void checkConnectionOnComponent() throws Exception {
-        MongoDbComponent component = context.getComponent("mongodb", MongoDbComponent.class);
-        MongoClient myDbS = applicationContext.getBean("myDbS", MongoClient.class);
-        component.setMongoConnection(myDbS);
-        Endpoint endpoint = component.createEndpoint("mongodb:justARouteName?database={{mongodb.testDb}}&collection="
-                + "{{mongodb.testCollection}}&operation=count&dynamicity=true");
+        Endpoint endpoint = context.getEndpoint("mongodb:justARouteName");
+
         assertIsInstanceOf(MongoDbEndpoint.class, endpoint);
-        assertEquals(myDbS, ((MongoDbEndpoint) endpoint).getMongoConnection());
+        assertEquals(mongo, ((MongoDbEndpoint) endpoint).getMongoConnection());
     }
-
 }
diff --git a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbContainer.java b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbContainer.java
new file mode 100644
index 0000000..1329805
--- /dev/null
+++ b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbContainer.java
@@ -0,0 +1,86 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ *      http://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.apache.camel.component.mongodb;
+
+
+import com.mongodb.client.MongoClient;
+import com.mongodb.client.MongoClients;
+import org.bson.Document;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.testcontainers.containers.GenericContainer;
+import org.testcontainers.containers.output.Slf4jLogConsumer;
+import org.testcontainers.containers.wait.strategy.Wait;
+
+public class MongoDbContainer extends GenericContainer {
+    private static final Logger LOGGER = LoggerFactory.getLogger(MongoDbContainer.class);
+    private static final String CONTAINER_NAME = "mongo";
+    private static final int MONGODB_PORT = 27017;
+    private static final String MONGO_IMAGE = "mongo:4.0";
+
+    public MongoDbContainer() {
+        super(MONGO_IMAGE);
+
+        setWaitStrategy(Wait.forListeningPort());
+
+        withNetworkAliases(CONTAINER_NAME);
+        withExposedPorts(MONGODB_PORT);
+        withLogConsumer(new Slf4jLogConsumer(LOGGER));
+        withCommand(
+            "--replSet", "replicationName",
+            "--oplogSize", "5000",
+            "--syncdelay", "0",
+            "--noauth",
+            "--noprealloc",
+            "--smallfiles");
+    }
+
+    @Override
+    public void start() {
+        super.start();
+
+        Document d = MongoClients.create(getConnectionURI())
+            .getDatabase("admin")
+            .runCommand(new Document("replSetInitiate", new Document()));
+
+        LOGGER.info("replSetInitiate: {}", d);
+        LOGGER.info("waiting to become master");
+
+        try {
+            execInContainer(
+                "/bin/bash",
+                "-c",
+                "until mongo --eval \"printjson(rs.isMaster())\" | grep ismaster | grep true > /dev/null 2>&1; do sleep 1; done");
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+
+        LOGGER.info("started");
+    }
+
+    public String getConnectionAddress() {
+        return getContainerIpAddress() + ":" + getMappedPort(MONGODB_PORT);
+    }
+
+    public String getConnectionURI() {
+        return "mongodb://" + getConnectionAddress();
+    }
+
+    public MongoClient createClient() {
+        return MongoClients.create(getConnectionURI());
+    }
+}
diff --git a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbOperationsTest.java b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbOperationsTest.java
index c875b2b..02c80ab 100644
--- a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbOperationsTest.java
+++ b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbOperationsTest.java
@@ -22,7 +22,6 @@ import java.util.HashMap;
 import java.util.List;
 
 import com.mongodb.MongoClientSettings;
-import com.mongodb.client.MongoClient;
 import com.mongodb.client.model.Filters;
 import com.mongodb.client.result.DeleteResult;
 import com.mongodb.client.result.UpdateResult;
@@ -42,7 +41,6 @@ import static com.mongodb.client.model.Filters.or;
 import static com.mongodb.client.model.Updates.combine;
 import static com.mongodb.client.model.Updates.currentTimestamp;
 import static com.mongodb.client.model.Updates.set;
-import static java.util.Arrays.asList;
 import static org.apache.camel.component.mongodb.MongoDbConstants.MONGO_ID;
 import static org.apache.camel.test.junit5.TestSupport.assertListSize;
 import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -95,7 +93,7 @@ public class MongoDbOperationsTest extends AbstractMongoDbTest {
     public void testStoreOidsOnInsert() throws Exception {
         Document firsDocument = new Document();
         Document secondDoocument = new Document();
-        List<?> oids = template.requestBody("direct:testStoreOidOnInsert", asList(firsDocument, secondDoocument), List.class);
+        List<?> oids = template.requestBody("direct:testStoreOidOnInsert", Arrays.asList(firsDocument, secondDoocument), List.class);
         assertTrue(oids.contains(firsDocument.get(MONGO_ID)));
         assertTrue(oids.contains(secondDoocument.get(MONGO_ID)));
     }
diff --git a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbSpringDslOperationsTest.java b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbSpringDslOperationsTest.java
index 4bb5e80..f4be369 100644
--- a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbSpringDslOperationsTest.java
+++ b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbSpringDslOperationsTest.java
@@ -19,15 +19,25 @@ package org.apache.camel.component.mongodb;
 import org.apache.camel.CamelContext;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.spring.SpringCamelContext;
-import org.springframework.context.annotation.AnnotationConfigApplicationContext;
+import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
+import org.springframework.context.support.GenericApplicationContext;
+import org.springframework.core.io.ClassPathResource;
 
 public class MongoDbSpringDslOperationsTest extends MongoDbOperationsTest {
 
     @Override
     protected CamelContext createCamelContext() throws Exception {
-        applicationContext = new AnnotationConfigApplicationContext(MongoBasicOperationsConfiguration.class);
+        GenericApplicationContext applicationContext = new GenericApplicationContext();
+        applicationContext.getBeanFactory().registerSingleton("myDb", mongo);
+
+        XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(applicationContext);
+        xmlReader.loadBeanDefinitions(new ClassPathResource("org/apache/camel/component/mongodb/mongoBasicOperationsTest.xml"));
+
+        applicationContext.refresh();
+
         @SuppressWarnings("deprecation")
         CamelContext ctx = SpringCamelContext.springCamelContext(applicationContext, true);
+
         return ctx;
     }
 
@@ -41,5 +51,4 @@ public class MongoDbSpringDslOperationsTest extends MongoDbOperationsTest {
             }
         };
     }
-
 }
diff --git a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/meta/MongoDbMetaExtensionTest.java b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/meta/MongoDbMetaExtensionTest.java
index 788a78f..fff9d47 100644
--- a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/meta/MongoDbMetaExtensionTest.java
+++ b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/meta/MongoDbMetaExtensionTest.java
@@ -35,7 +35,6 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 
 public class MongoDbMetaExtensionTest extends AbstractMongoDbTest {
-
     // We simulate the presence of an authenticated user
     @BeforeEach
     public void createAuthorizationUser() {
@@ -87,7 +86,7 @@ public class MongoDbMetaExtensionTest extends AbstractMongoDbTest {
         Map<String, Object> parameters = new HashMap<>();
         parameters.put("database", database);
         parameters.put("collection", collection);
-        parameters.put("host", HOST);
+        parameters.put("host", container.getConnectionAddress());
         parameters.put("user", USER);
         parameters.put("password", PASSWORD);
 
@@ -113,13 +112,12 @@ public class MongoDbMetaExtensionTest extends AbstractMongoDbTest {
         Map<String, Object> parameters = new HashMap<>();
         parameters.put("database", database);
         parameters.put("collection", collection);
-        parameters.put("host", HOST);
+        parameters.put("host", container.getConnectionAddress());
         parameters.put("user", USER);
         parameters.put("password", PASSWORD);
 
         // Then
         assertThrows(IllegalArgumentException.class, () -> {
-
             component.getExtension(MetaDataExtension.class).get().meta(parameters).orElseThrow(IllegalArgumentException::new);
         });
     }
@@ -148,7 +146,7 @@ public class MongoDbMetaExtensionTest extends AbstractMongoDbTest {
         Map<String, Object> parameters = new HashMap<>();
         parameters.put("database", database);
         parameters.put("collection", collection);
-        parameters.put("host", HOST);
+        parameters.put("host", container.getConnectionAddress());
         parameters.put("user", USER);
         parameters.put("password", PASSWORD);
 
diff --git a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/verifier/MongoDbVerifierExtensionTest.java b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/verifier/MongoDbVerifierExtensionTest.java
index e00d8bc..272644f 100644
--- a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/verifier/MongoDbVerifierExtensionTest.java
+++ b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/verifier/MongoDbVerifierExtensionTest.java
@@ -22,7 +22,6 @@ import java.util.Map;
 import org.apache.camel.Component;
 import org.apache.camel.component.extension.ComponentVerifierExtension;
 import org.apache.camel.component.mongodb.AbstractMongoDbTest;
-import org.apache.camel.component.mongodb.MongoDbComponent;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
@@ -30,7 +29,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
 public class MongoDbVerifierExtensionTest extends AbstractMongoDbTest {
-
     // We simulate the presence of an authenticated user
     @BeforeEach
     public void createAuthorizationUser() {
@@ -42,10 +40,6 @@ public class MongoDbVerifierExtensionTest extends AbstractMongoDbTest {
         return false;
     }
 
-    protected MongoDbComponent getComponent() {
-        return context().getComponent(SCHEME, MongoDbComponent.class);
-    }
-
     protected ComponentVerifierExtension getExtension() {
         Component component = context().getComponent(SCHEME);
         ComponentVerifierExtension verifier = component.getExtension(ComponentVerifierExtension.class).orElseThrow(IllegalStateException::new);
@@ -57,7 +51,7 @@ public class MongoDbVerifierExtensionTest extends AbstractMongoDbTest {
     public void verifyConnectionOK() {
         //When
         Map<String, Object> parameters = new HashMap<>();
-        parameters.put("host", HOST);
+        parameters.put("host", container.getConnectionAddress());
         parameters.put("user", USER);
         parameters.put("password", PASSWORD);
         //Given
@@ -84,7 +78,7 @@ public class MongoDbVerifierExtensionTest extends AbstractMongoDbTest {
     public void verifyConnectionMissingParams() {
         //When
         Map<String, Object> parameters = new HashMap<>();
-        parameters.put("host", HOST);
+        parameters.put("host", container.getConnectionAddress());
         parameters.put("user", USER);
         //Given
         ComponentVerifierExtension.Result result = getExtension().verify(ComponentVerifierExtension.Scope.PARAMETERS, parameters);
@@ -97,7 +91,7 @@ public class MongoDbVerifierExtensionTest extends AbstractMongoDbTest {
     public void verifyConnectionNotAuthenticated() {
         //When
         Map<String, Object> parameters = new HashMap<>();
-        parameters.put("host", HOST);
+        parameters.put("host", container.getConnectionAddress());
         parameters.put("user", USER);
         parameters.put("password", "wrongPassword");
         //Given
@@ -111,7 +105,7 @@ public class MongoDbVerifierExtensionTest extends AbstractMongoDbTest {
     public void verifyConnectionAdminDBKO() {
         //When
         Map<String, Object> parameters = new HashMap<>();
-        parameters.put("host", HOST);
+        parameters.put("host", container.getConnectionAddress());
         parameters.put("user", USER);
         parameters.put("password", PASSWORD);
         parameters.put("adminDB", "someAdminDB");
diff --git a/components/camel-mongodb/src/test/resources/log4j2.properties b/components/camel-mongodb/src/test/resources/log4j2.properties
index 9032125..0689548 100644
--- a/components/camel-mongodb/src/test/resources/log4j2.properties
+++ b/components/camel-mongodb/src/test/resources/log4j2.properties
@@ -24,6 +24,10 @@ appender.out.type = Console
 appender.out.name = out
 appender.out.layout.type = PatternLayout
 appender.out.layout.pattern = [%30.30t] %-30.30c{1} %-5p %m%n
-rootLogger.level = WARN
+
+logger.mongodb.name = org.apache.camel.component.mongodb.MongoDbContainer
+logger.mongodb.level = INFO
+
+rootLogger.level = INFO
 rootLogger.appenderRef.file.ref = file
 #rootLogger.appenderRef.out.ref = out
diff --git a/components/camel-mongodb/src/test/resources/mongodb.test.properties b/components/camel-mongodb/src/test/resources/mongodb.test.properties
index 89a9583..e61392f 100644
--- a/components/camel-mongodb/src/test/resources/mongodb.test.properties
+++ b/components/camel-mongodb/src/test/resources/mongodb.test.properties
@@ -15,7 +15,7 @@
 ## limitations under the License.
 ## ---------------------------------------------------------------------------
 
-mongodb.connectionURI=mongodb://localhost:27017
+
 mongodb.testDb=test
 mongodb.testCollection=camelTest
 mongodb.cappedTestCollection=camelTestCapped