[ Web Proxy ]
URL:
Viewing: https://java.testcontainers.org/supported_docker_environment/../modules/databases/mongodb/ [Back]  [Original]

MongoDB Module - Testcontainers for Java
Skip to content
Testcontainers [Testcontainers] Menu
testcontainers-java
Content

MongoDB Module

The MongoDB module provides two Testcontainers for MongoDB unit testing:

MongoDBContainer

Usage example

The following example shows how to create a MongoDBContainer:

Creating a MongoDB container
MongoDBContainer mongoDBContainer = new MongoDBContainer("mongo:4.0.10").withReplicaSet()

And how to start it:

Starting a MongoDB container
mongoDBContainer.start();

Note

To construct a multi-node MongoDB cluster, consider the mongodb-replica-set project

Motivation

Implement a reusable, cross-platform, simple to install solution that doesn't depend on fixed ports to test MongoDB transactions.

General info

MongoDB starting from version 4 supports multi-document transactions only for a replica set. For instance, to initialize a single node replica set on fixed ports via Docker, one has to do the following:

  • Run a MongoDB container of version 4 and up specifying --replSet command
  • Initialize a single replica set via executing a proper command
  • Wait for the initialization to complete
  • Provide a special url for a user to employ with a MongoDB driver without specifying replicaSet

As we can see, there is a lot of operations to execute and we even haven't touched a non-fixed port approach. That's where the MongoDBContainer might come in handy.

MongoDBAtlasLocalContainer

Usage example

The following example shows how to create a MongoDBAtlasLocalContainer:

Creating a MongoDB Atlas Local Container
MongoDBAtlasLocalContainer atlasLocalContainer = new MongoDBAtlasLocalContainer(
    "mongodb/mongodb-atlas-local:7.0.9"
);

And how to start it:

Start the Container
atlasLocalContainer.start();

The connection string provided by the MongoDBAtlasLocalContainer's getConnectionString() method includes the dynamically allocated port:

Get the Connection String
String connectionString = atlasLocalContainer.getConnectionString();

e.g. mongodb://localhost:12345/?directConnection=true

References

MongoDB Atlas Local combines the MongoDB database engine with MongoT, a sidecar process for advanced searching capabilities built by MongoDB and powered by Apache Lucene.

The container (mongodb/mongodb-atlas-local) documentation can be found here.

General information about Atlas Search can be found here.

Adding this module to your project dependencies

Add the following dependency to your pom.xml/build.gradle file:

GradleMaven
testImplementation "org.testcontainers:testcontainers-mongodb:2.0.5"
<dependency>
    <groupId>org.testcontainers</groupId>
    <artifactId>testcontainers-mongodb</artifactId>
    <version>2.0.5</version>
    <scope>test</scope>
</dependency>

Hint

Adding this Testcontainers library JAR will not automatically add a database driver JAR to your project. You should ensure that your project also has a suitable database driver as a dependency

Copyright (c) 2019 Konstantin Silaev silaev256@gmail.com


Web Proxy Viewer  |  New URL  |  Original Page