How to use MongoDBContainer class of org.testcontainers.containers package

Best Testcontainers-java code snippet using org.testcontainers.containers.MongoDBContainer

Source:MongoDBSteps.java Github

copy

Full Screen

...26import io.cucumber.datatable.DataTable;27import io.cucumber.java.Before;28import io.cucumber.java.Scenario;29import io.cucumber.java.en.Given;30import org.testcontainers.containers.MongoDBContainer;31import org.testcontainers.containers.wait.strategy.Wait;32import org.testcontainers.utility.DockerImageName;33import static com.consol.citrus.container.FinallySequence.Builder.doFinally;34import static java.time.temporal.ChronoUnit.SECONDS;35public class MongoDBSteps {36 @CitrusFramework37 private Citrus citrus;38 @CitrusResource39 private TestCaseRunner runner;40 @CitrusResource41 private TestContext context;42 private String mongoDBVersion = MongoDBSettings.getMongoDBVersion();43 private int startupTimeout = MongoDBSettings.getStartupTimeout();44 private MongoDBContainer mongoDBContainer;45 private Map<String, String> env = new HashMap<>();46 @Before47 public void before(Scenario scenario) {48 if (mongoDBContainer == null && citrus.getCitrusContext().getReferenceResolver().isResolvable(MongoDBContainer.class)) {49 mongoDBContainer = citrus.getCitrusContext().getReferenceResolver().resolve("mongoDBContainer", MongoDBContainer.class);50 setConnectionSettings(mongoDBContainer, context);51 }52 }53 @Given("^MongoDB version (^\\s+)$")54 public void setMongoDBVersion(String version) {55 this.mongoDBVersion = version;56 }57 @Given("^MongoDB startup timeout is (\\d+)(?: s| seconds)$")58 public void setStartupTimeout(int timeout) {59 this.startupTimeout = timeout;60 }61 @Given("^MongoDB env settings$")62 public void setEnvSettings(DataTable settings) {63 this.env.putAll(settings.asMap());64 }65 @Given("^start MongoDB container$")66 public void startMongo() {67 mongoDBContainer = new MongoDBContainer(DockerImageName.parse("mongo").withTag(mongoDBVersion))68 .withLabel("app", "yaks")69 .withLabel("app.kubernetes.io/name", "mongodb")70 .withLabel("app.kubernetes.io/part-of", TestContainersSettings.getTestName())71 .withLabel("app.openshift.io/connects-to", TestContainersSettings.getTestId())72 .withNetworkAliases("mongodb")73 .withEnv(env)74 .waitingFor(Wait.forLogMessage("(?i).*waiting for connections.*", 1)75 .withStartupTimeout(Duration.of(startupTimeout, SECONDS)));76 mongoDBContainer.start();77 citrus.getCitrusContext().bind("mongoDBContainer", mongoDBContainer);78 setConnectionSettings(mongoDBContainer, context);79 if (TestContainersSteps.autoRemoveResources) {80 runner.run(doFinally()81 .actions(context -> mongoDBContainer.stop()));82 }83 }84 @Given("^stop MongoDB container$")85 public void stopMongo() {86 if (mongoDBContainer != null) {87 mongoDBContainer.stop();88 }89 env = new HashMap<>();90 }91 /**92 * Sets the connection settings in current test context in the form of test variables.93 * @param mongoDBContainer94 * @param context95 */96 private void setConnectionSettings(MongoDBContainer mongoDBContainer, TestContext context) {97 if (mongoDBContainer.isRunning()) {98 String containerId = mongoDBContainer.getContainerId().substring(0, 12);99 context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "MONGODB_CONTAINER_IP", mongoDBContainer.getContainerIpAddress());100 context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "MONGODB_CONTAINER_ID", containerId);101 context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "MONGODB_CONTAINER_NAME", mongoDBContainer.getContainerName());102 context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "MONGODB_SERVICE_NAME", "kd-" + containerId);103 context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "MONGODB_URL", mongoDBContainer.getReplicaSetUrl());104 }105 }106}...

Full Screen

Full Screen

Source:BaseIntegrationTest.java Github

copy

Full Screen

1package pl.tkaras.config;2import org.springframework.boot.test.context.SpringBootTest;3import org.springframework.test.context.DynamicPropertyRegistry;4import org.springframework.test.context.DynamicPropertySource;5import org.testcontainers.containers.MongoDBContainer;6import org.testcontainers.containers.Network;7import org.testcontainers.junit.jupiter.Testcontainers;8import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;9@Testcontainers10@SpringBootTest(webEnvironment = RANDOM_PORT)11public abstract class BaseIntegrationTest {12 private static final MongoDBContainer mongoDBContainer;13 private static final Network network;14 static {15 network = Network.newNetwork();16 mongoDBContainer = new MongoDBContainer("mongo:4.2")17 .withNetwork(network);18 mongoDBContainer.start();19 }20 @DynamicPropertySource21 static void setProperties(DynamicPropertyRegistry registry){22 registry.add("spring.data.mongodb.uri", mongoDBContainer::getReplicaSetUrl);23 }24}...

Full Screen

Full Screen

Source:AbstractIntegrationTest.java Github

copy

Full Screen

1package de.ksbrwsk.people;2import org.springframework.test.context.DynamicPropertyRegistry;3import org.springframework.test.context.DynamicPropertySource;4import org.testcontainers.containers.MongoDBContainer;5import org.testcontainers.junit.jupiter.Container;6import org.testcontainers.junit.jupiter.Testcontainers;7@Testcontainers8public class AbstractIntegrationTest {9 @Container10 static MongoDBContainer mongoDBContainer = new MongoDBContainer("mongo:4.4.2");11 @DynamicPropertySource12 static void setProperties(DynamicPropertyRegistry registry) {13 registry.add("spring.data.mongodb.host", mongoDBContainer::getHost);14 registry.add("spring.data.mongodb.port", mongoDBContainer::getFirstMappedPort);15 }16}...

Full Screen

Full Screen

MongoDBContainer

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.MongoDBContainer;2import org.testcontainers.containers.GenericContainer;3import org.testcontainers.containers.Network;4import org.testcontainers.containers.output.Slf4jLogConsumer;5import org.testcontainers.containers.wait.strategy.Wait;6import org.testcontainers.utility.DockerImageName;7import org.testcontainers.utility.MountableFile;8import org.slf4j.Logger;9import org.slf4j.LoggerFactory;10import java.io.File;11import java.io.IOException;12import java.util.ArrayList;13import java.util.HashMap;14import java.util.List;15import java.util.Map;16import java.util.stream.Collectors;17public class MongoDbContainer {18 private static final Logger LOGGER = LoggerFactory.getLogger(MongoDbContainer.class);19 private static final String MONGO_IMAGE = "mongo:4.0.4";20 private static final String MONGO_INIT_SCRIPT = "mongo-init.js";21 private static final String MONGO_INIT_SCRIPT_DEST = "/docker-entrypoint-initdb.d/" + MONGO_INIT_SCRIPT;22 private static final String MONGO_INIT_SCRIPT_TEMPLATE = "mongo-init-template.js";23 private static final String MONGO_INIT_SCRIPT_TEMPLATE_DEST = "/docker-entrypoint-initdb.d/" + MONGO_INIT_SCRIPT_TEMPLATE;24 private static final String MONGO_INIT_SCRIPT_TEMPLATE_DEST_IN_CONTAINER = "/tmp/" + MONGO_INIT_SCRIPT_TEMPLATE;25 private static final String MONGO_INIT_SCRIPT_DEST_IN_CONTAINER = "/tmp/" + MONGO_INIT_SCRIPT;26 private static final String MONGO_INIT_SCRIPT_DEST_IN_CONTAINER_IN_CONTAINER = "/docker-entrypoint-initdb.d/" + MONGO_INIT_SCRIPT;27 private static final String MONGO_INIT_SCRIPT_TEMPLATE_DEST_IN_CONTAINER_IN_CONTAINER = "/docker-entrypoint-initdb.d/" + MONGO_INIT_SCRIPT_TEMPLATE;28 private static final String MONGO_INIT_SCRIPT_TEMPLATE_DEST_IN_CONTAINER_IN_CONTAINER_OLD = "/docker-entrypoint-initdb.d/" + MONGO_INIT_SCRIPT_TEMPLATE;29 private static final String MONGO_INIT_SCRIPT_DEST_IN_CONTAINER_IN_CONTAINER_OLD = "/docker-entrypoint-initdb.d/" + MONGO_INIT_SCRIPT;30 private static final String MONGO_INIT_SCRIPT_DEST_IN_CONTAINER_IN_CONTAINER_OLD_2 = "/docker-entrypoint-initdb.d/" + MONGO_INIT_SCRIPT;31 private static final String MONGO_INIT_SCRIPT_DEST_IN_CONTAINER_IN_CONTAINER_OLD_3 = "/docker-entrypoint-initdb.d/" + MONGO_INIT_SCRIPT;

Full Screen

Full Screen

MongoDBContainer

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.MongoDBContainer;2public class MongoTestContainer {3 public static void main(String[] args) {4 MongoDBContainer mongoDBContainer = new MongoDBContainer();5 mongoDBContainer.start();6 System.out.println("MongoDBContainer is started!");7 System.out.println("MongoDBContainer is running on port: " + mongoDBContainer.getFirstMappedPort());8 System.out.println("MongoDBContainer is running on host: " + mongoDBContainer.getContainerIpAddress());9 System.out.println("MongoDBContainer is running on database: " + mongoDBContainer.getDatabaseName());10 System.out.println("MongoDBContainer is running on username: " + mongoDBContainer.getUsername());11 System.out.println("MongoDBContainer is running on password: " + mongoDBContainer.getPassword());12 mongoDBContainer.stop();13 System.out.println("MongoDBContainer is stopped!");14 }15}

Full Screen

Full Screen

MongoDBContainer

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.MongoDBContainer;2public class MongoDbContainerTest {3 public static void main(String[] args) {4 MongoDBContainer mongoDBContainer = new MongoDBContainer();5 mongoDBContainer.start();6 System.out.println("MongoDBContainer started");7 System.out.println("MongoDBContainer host name: " + mongoDBContainer.getContainerIpAddress());8 System.out.println("MongoDBContainer port: " + mongoDBContainer.getFirstMappedPort());9 mongoDBContainer.stop();10 System.out.println("MongoDBContainer stopped");11 }12}

Full Screen

Full Screen

MongoDBContainer

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.MongoDBContainer;2import org.testcontainers.containers.wait.strategy.Wait;3public class MongoDbContainer extends MongoDBContainer {4 private static MongoDbContainer container;5 private MongoDbContainer() {6 super("mongo:4.0.10");7 }8 public static MongoDbContainer getInstance() {9 if (container == null) {10 container = new MongoDbContainer();11 }12 return container;13 }14 public void start() {15 super.start();16 System.setProperty("MONGO_DB_URL", container.getReplicaSetUrl());17 }18 public void stop() {19 }20}21import org.testcontainers.containers.MongoDBContainer;22import org.testcontainers.containers.wait.strategy.Wait;23public class MongoDbContainer extends MongoDBContainer {24 private static MongoDbContainer container;25 private MongoDbContainer() {26 super("mongo:4.0.10");27 }28 public static MongoDbContainer getInstance() {29 if (container == null) {30 container = new MongoDbContainer();31 }32 return container;33 }34 public void start() {35 super.start();36 System.setProperty("MONGO_DB_URL", container.getReplicaSetUrl());37 }38 public void stop() {39 }40}41import org.testcontainers.containers.MongoDBContainer;42import org.testcontainers.containers.wait.strategy.Wait;43public class MongoDbContainer extends MongoDBContainer {44 private static MongoDbContainer container;45 private MongoDbContainer() {46 super("mongo:4.0.10");47 }48 public static MongoDbContainer getInstance() {49 if (container == null) {50 container = new MongoDbContainer();51 }52 return container;53 }54 public void start() {55 super.start();56 System.setProperty("MONGO_DB_URL", container.getReplicaSetUrl());57 }58 public void stop() {59 }60}61import org.testcontainers.containers.MongoDBContainer;62import org.testcontainers.cont

Full Screen

Full Screen

MongoDBContainer

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.MongoDBContainer;2public class Main {3 public static void main(String[] args) {4 MongoDBContainer container = new MongoDBContainer("mongo:4.0.10");5 container.start();6 String connectionString = container.getReplicaSetUrl();7 System.out.println("Connection String: " + connectionString);8 }9}

Full Screen

Full Screen

MongoDBContainer

Using AI Code Generation

copy

Full Screen

1import com.mongodb.MongoClient;2import org.junit.Test;3import org.testcontainers.containers.MongoDBContainer;4public class MongoDbTest {5 public void testMongoDb() {6 try (MongoDBContainer mongoDBContainer = new MongoDBContainer()) {7 mongoDBContainer.start();8 MongoClient mongoClient = new MongoClient(mongoDBContainer.getContainerIpAddress(), mongoDBContainer.getFirstMappedPort());9 }10 }11}12[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ testcontainers ---13[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ testcontainers ---14[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ testcontainers ---15[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ testcontainers ---16[main] INFO org.testcontainers.utility.RegistryAuthLocator - Credential helper/store (docker-credential-desktop) does not have credentials for index.docker.io17[main] INFO org.testcontainers.utility.RegistryAuthLocator - Credential helper/store (docker

Full Screen

Full Screen

MongoDBContainer

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.MongoDBContainer;2import org.testcontainers.utility.DockerImageName;3public class TestMongoDBContainer {4 public static void main(String[] args) {5 MongoDBContainer mongoDBContainer = new MongoDBContainer(DockerImageName.parse("mongo:latest"));6 mongoDBContainer.start();7 String url = mongoDBContainer.getReplicaSetUrl();8 System.out.println("Connection URL: " + url);9 mongoDBContainer.stop();10 }11}

Full Screen

Full Screen

MongoDBContainer

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.MongoDBContainer;2public class MongoDBContainerExample {3 public static void main(String[] args) {4 MongoDBContainer mongoDBContainer = new MongoDBContainer("mongo:4.0.10");5 mongoDBContainer.start();6 System.out.println("Connection string: " + mongoDBContainer.getReplicaSetUrl());7 mongoDBContainer.stop();8 }9}10In the above code, we have used the getReplicaSetUrl() method to get the connection string. We can also use the getDockerImageName() , getHost() , getMappedPort() , getPortBindings() , getReplicaSetName() , getUri() , getContainerId() , getContainerIpAddress() , getExposedPorts() , getNetwork() , getNetworkAliases() , getNetworkMode() , getNetworkSettings() , getEnvMap() , getEnvMap() , getEnv() , getLabels() , getLabel() , getLogConsumer() , getLogConsumers() , getExposedHostPort() , getExposedContainerPort() , getLogConsumer() , getLogConsumers()

Full Screen

Full Screen

MongoDBContainer

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.MongoDBContainer;2import org.testcontainers.containers.GenericContainer;3{4 public static void main(String[] args)5 {6 MongoDBContainer mongoDBContainer = new MongoDBContainer();7 mongoDBContainer.start();8 System.out.println(mongoDBContainer.getContainerIpAddress());9 System.out.println(mongoDBContainer.getMappedPort(27017));10 System.out.println(mongoDBContainer.getMongoDbHost());11 System.out.println(mongoDBContainer.getMongoDbPort());12 System.out.println(mongoDBContainer.getDatabaseName());13 System.out.println(mongoDBContainer.getUsername());14 System.out.println(mongoDBContainer.getPassword());15 mongoDBContainer.stop();16 }17}

Full Screen

Full Screen

MongoDBContainer

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.MongoDBContainer;2public class Test {3 public static void main(String[] args) {4 MongoDBContainer container = new MongoDBContainer("mongo:4.2.6");5 container.start();6 System.out.println(container.getReplicaSetUrl());7 container.stop();8 }9}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Testcontainers-java automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful