Class MockServerContainer

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

public class MockServerContainer extends org.testcontainers.containers.GenericContainer<MockServerContainer>
An enriched Testcontainers module for MockServer.

Unlike the upstream org.testcontainers:mockserver module, this module:

  • Derives the Docker image tag from the client jar version so it stays in lockstep
  • Supports DNS, transparent proxy, HTTP/3, initialization JSON, and other configuration helpers
  • Provides direct MockServerClient wiring via getClient()
  • Nested Class Summary

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

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

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Default MockServer port (HTTP, HTTPS, SOCKS, and HTTP CONNECT are all served on a single unified port).

    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
    Creates a MockServerContainer with the default image tag derived from the client jar version.
    MockServerContainer(org.testcontainers.utility.DockerImageName dockerImageName)
    Creates a MockServerContainer with a custom Docker image.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Stops the cached client (if any) and the container.
    Returns a MockServerClient connected to this container.
    Returns the HTTP endpoint URL for the MockServer container.
    Returns the HTTPS endpoint URL for the MockServer container.
    int
    Returns the mapped server port on the host.
    withDnsPort(int dnsPort)
    Enables DNS resolution on the specified UDP port.
    withHttp3(int udpPort)
    Enables experimental HTTP/3 (QUIC) support on the specified UDP port.
    withInitializationJson(String hostInitJsonPath)
    Copies an initialization JSON file into the container and configures MockServer to load its expectations at startup.
    Sets the MockServer log level.
    Sets multiple MockServer properties as environment variables.
    withProperty(String key, String value)
    Sets a single MockServer property as an environment variable.
    withServerPort(int port)
    Sets a custom server port.
    Enables transparent proxy mode and adds the NET_ADMIN Linux capability.

    Methods inherited from class org.testcontainers.containers.GenericContainer

    addEnv, addExposedPort, addExposedPorts, addFileSystemBind, addFixedExposedPort, addFixedExposedPort, addLink, apply, canBeReused, configure, containerIsCreated, containerIsStarted, containerIsStarted, containerIsStarting, 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
  • Field Details

    • PORT

      public static final int PORT
      Default MockServer port (HTTP, HTTPS, SOCKS, and HTTP CONNECT are all served on a single unified port).
      See Also:
  • Constructor Details

    • MockServerContainer

      public MockServerContainer()
      Creates a MockServerContainer with the default image tag derived from the client jar version.
    • MockServerContainer

      public MockServerContainer(org.testcontainers.utility.DockerImageName dockerImageName)
      Creates a MockServerContainer with a custom Docker image.
      Parameters:
      dockerImageName - the Docker image to use (must be compatible with mockserver/mockserver)
  • Method Details

    • getServerPort

      public int getServerPort()
      Returns the mapped server port on the host.
      Returns:
      the host port mapped to the MockServer container port
    • getEndpoint

      public String getEndpoint()
      Returns the HTTP endpoint URL for the MockServer container.
      Returns:
      the HTTP endpoint in the form http://host:port
    • getSecureEndpoint

      public String getSecureEndpoint()
      Returns the HTTPS endpoint URL for the MockServer container. MockServer serves HTTP and HTTPS on the same unified port.
      Returns:
      the HTTPS endpoint in the form https://host:port
    • getClient

      public MockServerClient getClient()
      Returns a MockServerClient connected to this container. The client is created lazily on first call and cached; it is closed automatically when the container stops.
      Returns:
      a MockServerClient connected to the running container
    • close

      public void close()
      Stops the cached client (if any) and the container. The cached client is stopped gracefully, which sends PUT /mockserver/stop and waits up to 10 seconds for confirmation before the container itself is stopped.
    • withServerPort

      public MockServerContainer withServerPort(int port)
      Sets a custom server port. This changes the SERVER_PORT env var and replaces the exposed port so the listening-port wait strategy does not wait on a port MockServer is not listening on.
      Parameters:
      port - the port MockServer should listen on inside the container
      Returns:
      this container instance for chaining
    • withDnsPort

      public MockServerContainer withDnsPort(int dnsPort)
      Enables DNS resolution on the specified UDP port.

      Sets MOCKSERVER_DNS_ENABLED=true and MOCKSERVER_DNS_PORT to the given port. The DNS port is exposed as UDP.

      Parameters:
      dnsPort - the UDP port for DNS resolution
      Returns:
      this container instance for chaining
    • withTransparentProxy

      public MockServerContainer withTransparentProxy()
      Enables transparent proxy mode and adds the NET_ADMIN Linux capability.

      Note: iptables/redirect rules are the operator's responsibility; this helper only enables the mode and grants the required capability.

      Returns:
      this container instance for chaining
    • withProperties

      public MockServerContainer withProperties(Map<String,String> properties)
      Sets multiple MockServer properties as environment variables.

      Keys must be in the MockServer environment variable form (e.g. MOCKSERVER_LOG_LEVEL).

      Parameters:
      properties - a map of MockServer env-var keys to their values
      Returns:
      this container instance for chaining
    • withProperty

      public MockServerContainer withProperty(String key, String value)
      Sets a single MockServer property as an environment variable.

      The key must be in the MockServer environment variable form (e.g. MOCKSERVER_LOG_LEVEL).

      Parameters:
      key - the MockServer env-var key
      value - the value to set
      Returns:
      this container instance for chaining
    • withInitializationJson

      public MockServerContainer withInitializationJson(String hostInitJsonPath)
      Copies an initialization JSON file into the container and configures MockServer to load its expectations at startup.

      This is one-shot startup loading (MockServer's initializationJsonPath), not ongoing persistence of expectations across restarts (which is the separate persistExpectations setting). The file is copied to /config/initializerJson.json and MOCKSERVER_INITIALIZATION_JSON_PATH is pointed at it.

      Parameters:
      hostInitJsonPath - the path on the host to the initialization JSON file
      Returns:
      this container instance for chaining
    • withLogLevel

      public MockServerContainer withLogLevel(String level)
      Sets the MockServer log level.
      Parameters:
      level - the log level (e.g. "INFO", "DEBUG", "WARN", "ERROR", "TRACE")
      Returns:
      this container instance for chaining
    • withHttp3

      public MockServerContainer withHttp3(int udpPort)
      Enables experimental HTTP/3 (QUIC) support on the specified UDP port.

      Sets MOCKSERVER_HTTP3_PORT and exposes the port as UDP.

      Parameters:
      udpPort - the UDP port for HTTP/3
      Returns:
      this container instance for chaining