Class MongoDBContainer

java.lang.Object
org.testcontainers.containers.FailureDetectingExternalResource
org.testcontainers.containers.GenericContainer<MongoDBContainer>
org.testcontainers.containers.MongoDBContainer
All Implemented Interfaces:
AutoCloseable, org.junit.rules.TestRule, org.testcontainers.containers.Container<MongoDBContainer>, org.testcontainers.containers.ContainerState, org.testcontainers.containers.traits.LinkableContainer, org.testcontainers.containers.wait.strategy.WaitStrategyTarget, org.testcontainers.lifecycle.Startable

public class MongoDBContainer extends org.testcontainers.containers.GenericContainer<MongoDBContainer>
Testcontainers implementation for MongoDB.

Supported images: mongo, mongodb/mongodb-community-server, mongodb/mongodb-enterprise-server

Exposed ports: 27017

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
     

    Nested classes/interfaces inherited from interface org.testcontainers.containers.Container

    org.testcontainers.containers.Container.ExecResult
  • Field Summary

    Fields inherited from class org.testcontainers.containers.GenericContainer

    CONTAINER_RUNNING_TIMEOUT_SEC, dependencies, dockerClient, INTERNAL_HOST_HOSTNAME, waitStrategy

    Fields inherited from interface org.testcontainers.containers.ContainerState

    STATE_HEALTHY
  • Constructor Summary

    Constructors
    Constructor
    Description
    Deprecated.
    MongoDBContainer(@NonNull String dockerImageName)
     
    MongoDBContainer(org.testcontainers.utility.DockerImageName dockerImageName)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    containerIsStarted(com.github.dockerjava.api.command.InspectContainerResponse containerInfo, boolean reused)
     
    protected void
    containerIsStarting(com.github.dockerjava.api.command.InspectContainerResponse containerInfo)
     
    Gets a connection string url, unlike getReplicaSetUrl() this does not point to a database
    Gets a replica set url for the default "test" database.
    getReplicaSetUrl(String databaseName)
    Gets a replica set url for a provided databaseName.
    Enables sharding on the cluster

    Methods inherited from class org.testcontainers.containers.GenericContainer

    addEnv, addExposedPort, addExposedPorts, addFileSystemBind, addFixedExposedPort, addFixedExposedPort, addLink, apply, canBeReused, configure, containerIsCreated, containerIsStarted, containerIsStarting, containerIsStopped, containerIsStopping, copyFileFromContainer, createVolumeDirectory, dependsOn, dependsOn, dependsOn, doStart, equals, failed, finished, getBinds, getCommandParts, getContainerId, getContainerInfo, getContainerName, getCopyToFileContainerPathMap, getCreateContainerCmdModifiers, getDependencies, getDockerClient, getDockerImageName, getEnv, getEnvMap, getExposedPorts, getExtraHosts, getImage, getIpAddress, getLabels, getLinkedContainers, getLivenessCheckPort, getLivenessCheckPortNumbers, getLivenessCheckPorts, getLogConsumers, getNetwork, getNetworkAliases, getNetworkMode, getPortBindings, getShmSize, getStartupAttempts, getStartupCheckStrategy, getTestHostIpAddress, getTmpFsMapping, getVolumesFroms, getWaitStrategy, getWorkingDirectory, hashCode, isHostAccessible, isPrivilegedMode, isShouldBeReused, logger, setBinds, setCommand, setCommand, setCommandParts, setContainerDef, setCopyToFileContainerPathMap, setDockerImageName, setEnv, setExposedPorts, setExtraHosts, setHostAccessible, setImage, setLabels, setLinkedContainers, setLogConsumers, setNetwork, setNetworkAliases, setNetworkMode, setPortBindings, setPrivilegedMode, setShmSize, setStartupAttempts, setStartupCheckStrategy, setTmpFsMapping, setVolumesFroms, setWaitStrategy, setWorkingDirectory, start, starting, stop, succeeded, toString, waitingFor, waitUntilContainerStarted, withAccessToHost, withClasspathResourceMapping, withClasspathResourceMapping, withCommand, withCommand, withCopyFileToContainer, withCopyToContainer, withCreateContainerCmdModifier, withEnv, withEnv, withExposedPorts, withExtraHost, withFileSystemBind, withImagePullPolicy, withLabel, withLabels, withLogConsumer, withMinimumRunningDuration, withNetwork, withNetworkAliases, withNetworkMode, withPrivilegedMode, withReuse, withSharedMemorySize, withStartupAttempts, withStartupCheckStrategy, withStartupTimeout, withTmpFs, withVolumesFrom, withWorkingDirectory

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface org.testcontainers.containers.Container

    addFileSystemBind, followOutput, followOutput, self, withEnv, withFileSystemBind

    Methods inherited from interface org.testcontainers.containers.ContainerState

    copyFileFromContainer, copyFileToContainer, copyFileToContainer, execInContainer, execInContainer, execInContainer, execInContainer, execInContainerWithUser, execInContainerWithUser, getBoundPortNumbers, getContainerIpAddress, getCurrentContainerInfo, getFirstMappedPort, getHost, getLogs, getLogs, getMappedPort, isCreated, isHealthy, isRunning

    Methods inherited from interface org.testcontainers.lifecycle.Startable

    close
  • Constructor Details

    • MongoDBContainer

      @Deprecated public MongoDBContainer()
      Deprecated.
    • MongoDBContainer

      public MongoDBContainer(@NonNull @NonNull String dockerImageName)
    • MongoDBContainer

      public MongoDBContainer(org.testcontainers.utility.DockerImageName dockerImageName)
  • Method Details

    • containerIsStarting

      protected void containerIsStarting(com.github.dockerjava.api.command.InspectContainerResponse containerInfo)
      Overrides:
      containerIsStarting in class org.testcontainers.containers.GenericContainer<MongoDBContainer>
    • withSharding

      public MongoDBContainer withSharding()
      Enables sharding on the cluster
      Returns:
      this
    • containerIsStarted

      protected void containerIsStarted(com.github.dockerjava.api.command.InspectContainerResponse containerInfo, boolean reused)
      Overrides:
      containerIsStarted in class org.testcontainers.containers.GenericContainer<MongoDBContainer>
    • getConnectionString

      public String getConnectionString()
      Gets a connection string url, unlike getReplicaSetUrl() this does not point to a database
      Returns:
      a connection url pointing to a mongodb instance
    • getReplicaSetUrl

      public String getReplicaSetUrl()
      Gets a replica set url for the default "test" database.
      Returns:
      a replica set url.
    • getReplicaSetUrl

      public String getReplicaSetUrl(String databaseName)
      Gets a replica set url for a provided databaseName.
      Parameters:
      databaseName - a database name.
      Returns:
      a replica set url.