Class 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
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
MockServerClientwiring viagetClient()
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.testcontainers.containers.Container
org.testcontainers.containers.Container.ExecResult -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intDefault 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, waitStrategyFields inherited from interface org.testcontainers.containers.ContainerState
STATE_HEALTHY -
Constructor Summary
ConstructorsConstructorDescriptionCreates 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 TypeMethodDescriptionvoidclose()Stops the cached client (if any) and the container.Returns aMockServerClientconnected to this container.Returns the HTTP endpoint URL for the MockServer container.Returns the HTTPS endpoint URL for the MockServer container.intReturns 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.withLogLevel(String level) Sets the MockServer log level.withProperties(Map<String, String> properties) 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 theNET_ADMINLinux 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, withWorkingDirectoryMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface org.testcontainers.containers.Container
addFileSystemBind, followOutput, followOutput, self, withEnv, withFileSystemBindMethods 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 PORTDefault 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 withmockserver/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
Returns the HTTP endpoint URL for the MockServer container.- Returns:
- the HTTP endpoint in the form
http://host:port
-
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
Returns aMockServerClientconnected 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 sendsPUT /mockserver/stopand waits up to 10 seconds for confirmation before the container itself is stopped. -
withServerPort
Sets a custom server port. This changes theSERVER_PORTenv 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
Enables DNS resolution on the specified UDP port.Sets
MOCKSERVER_DNS_ENABLED=trueandMOCKSERVER_DNS_PORTto 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
Enables transparent proxy mode and adds theNET_ADMINLinux 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
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
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 keyvalue- the value to set- Returns:
- this container instance for chaining
-
withInitializationJson
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 separatepersistExpectationssetting). The file is copied to/config/initializerJson.jsonandMOCKSERVER_INITIALIZATION_JSON_PATHis pointed at it.- Parameters:
hostInitJsonPath- the path on the host to the initialization JSON file- Returns:
- this container instance for chaining
-
withLogLevel
Sets the MockServer log level.- Parameters:
level- the log level (e.g. "INFO", "DEBUG", "WARN", "ERROR", "TRACE")- Returns:
- this container instance for chaining
-
withHttp3
Enables experimental HTTP/3 (QUIC) support on the specified UDP port.Sets
MOCKSERVER_HTTP3_PORTand exposes the port as UDP.- Parameters:
udpPort- the UDP port for HTTP/3- Returns:
- this container instance for chaining
-