All deployment options (except the deployable WAR) use a single port for HTTP, HTTPS, and SOCKS traffic via port unification. You do not need to configure separate ports for different protocols.
- Docker
- Helm / Kubernetes
- Maven plugin
- npm / Node.js
- Standalone JAR
- JUnit 4
- JUnit 5
- Spring
Run MockServer as a Docker container:MockServer is now available at Docker ComposeAdd MockServer as a service in your To mount a properties file or JSON expectation initializer:
docker run -d --rm -p 1080:1080 mockserver/mockserver
http://localhost:1080.Configure via environment variablesPass configuration options as environment variables:docker run -d --rm \
-p 1090:1090 \
--env MOCKSERVER_LOG_LEVEL=TRACE \
--env MOCKSERVER_SERVER_PORT=1090 \
mockserver/mockserver
docker-compose.yml:version: "2.4"
services:
mockServer:
image: mockserver/mockserver:5.15.0
ports:
- 1080:1080
environment:
MOCKSERVER_MAX_EXPECTATIONS: 100
MOCKSERVER_MAX_HEADER_SIZE: 8192
version: "2.4"
services:
mockServer:
image: mockserver/mockserver:5.15.0
ports:
- 1080:1080
environment:
MOCKSERVER_PROPERTY_FILE: /config/mockserver.properties
MOCKSERVER_INITIALIZATION_JSON_PATH: /config/initializerJson.json
volumes:
- type: bind
source: .
target: /config
MockServer uses a distroless base image (no interactive shell) for reduced attack surface. Only the JVM and MockServer code are included in the container.
Deploy MockServer to any Kubernetes cluster using the official Helm chart.Add the chart repositoryInstall the chartAfter installation, MockServer is available inside the cluster at Supported values include:
Check deployment statusView logsAccess from outside the cluster (port-forward)Remove the chart
helm repo add mockserver https://www.mock-server.com
helm repo update
helm upgrade --install \
--kube-context <your kube context> \
--namespace mockserver \
--create-namespace \
--version 5.15.0 \
mockserver mockserver/mockserver
mockserver.mockserver.svc.cluster.local.Configure the deploymentPass configuration values with --set:helm upgrade --install \
--namespace mockserver \
--set app.serverPort=1080 \
--set app.logLevel=INFO \
mockserver mockserver/mockserver
| Value | Default | Description |
|---|---|---|
app.serverPort | 1080 | Port MockServer listens on |
app.logLevel | INFO | Log verbosity |
app.proxyRemoteHost | — | Host to forward proxy requests to |
app.proxyRemotePort | — | Port to forward proxy requests to |
app.jvmOptions | — | Additional JVM options |
image.snapshot | false | Use the latest snapshot image |
kubectl -n mockserver rollout status deployments mockserver
kubectl -n mockserver logs --tail=100 -l app=mockserver,release=mockserver
kubectl -n mockserver port-forward svc/mockserver 1080:1080 &
export MOCKSERVER_HOST=127.0.0.1:1080
helm delete -n mockserver mockserver
Use the Maven plugin to start MockServer automatically as part of your build lifecycle.Add the plugin to your This starts MockServer in the Or start MockServer as a forked JVM:Stop the forked instance:Use the Java client APITo control MockServer programmatically, add the client dependency:Then start and stop MockServer in your test lifecycle:
pom.xml:<plugin>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-maven-plugin</artifactId>
<version>5.15.0</version>
<configuration>
<serverPort>1080</serverPort>
<logLevel>DEBUG</logLevel>
<initializationClass>org.mockserver.maven.ExampleInitializationClass</initializationClass>
</configuration>
<executions>
<execution>
<id>process-test-classes</id>
<phase>process-test-classes</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>verify</id>
<phase>verify</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
process-test-classes phase and stops it in the verify phase, so your tests can use it during the test and integration-test phases.Run from the command linemvn mockserver:run
mvn -Dmockserver.serverPort=1080 -Dmockserver.logLevel=INFO \
org.mock-server:mockserver-maven-plugin:5.15.0:runForked
mvn -Dmockserver.serverPort=1080 \
org.mock-server:mockserver-maven-plugin:5.15.0:stopForked
If you use the
runForked goal and a test fails, Maven skips the verify phase, so MockServer will not be stopped. Use the start and stop goals instead to ensure MockServer always shuts down, even on test failures.<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-netty-no-dependencies</artifactId>
<version>5.15.0</version>
</dependency>
import static org.mockserver.integration.ClientAndServer.startClientAndServer;
private ClientAndServer mockServer;
@Before
public void startMockServer() {
mockServer = startClientAndServer(1080);
}
@After
public void stopMockServer() {
mockServer.stop();
}
Install the npm module:Start and stop from Node.jsGrunt pluginAdd
npm install mockserver-node --save-dev
var mockserver = require('mockserver-node');
mockserver.start_mockserver({
serverPort: 1080,
verbose: true,
trace: true
});
// run tests
mockserver.stop_mockserver({
serverPort: 1080
});
start_mockserver and stop_mockserver tasks to your Gruntfile:grunt.initConfig({
start_mockserver: {
start: {
options: {
serverPort: 1080,
trace: true
}
}
},
stop_mockserver: {
stop: {
options: {
serverPort: 1080
}
}
}
});
grunt.loadNpmTasks('mockserver-node');
The request log is only captured when
trace: true or verbose: true is set. You must enable one of these options to use the /retrieve endpoint for verification.Download Available options
Example — bind two ports with INFO loggingHomebrew (macOS)
mockserver-netty-no-dependencies-5.15.0.jar from Maven Central, then run:java -jar mockserver-netty-no-dependencies-5.15.0.jar -serverPort 1080
java -jar mockserver-netty-no-dependencies-5.15.0.jar \
-serverPort <port> \
[-proxyRemotePort <port>] \
[-proxyRemoteHost <hostname>] \
[-logLevel <level>] \
[-jvmOptions <system parameters>]
| Option | Required | Description |
|---|---|---|
-serverPort | Yes | HTTP, HTTPS, and SOCKS port(s). Supports comma-separated list for multiple ports. |
-proxyRemotePort | No | Enables port forwarding. Requests are forwarded to this port unless they match an expectation. |
-proxyRemoteHost | No | Host for forwarded proxy requests. Defaults to localhost if not set. |
-logLevel | No | SLF4J levels: TRACE, DEBUG, INFO, WARN, ERROR, OFF. Defaults to INFO. |
-jvmOptions | No | Generic JVM options or system properties. |
java -jar mockserver-netty-no-dependencies-5.15.0.jar \
-serverPort 1080,1081 \
-logLevel INFO
brew install mockserver
mockserver -serverPort 1080
Add the JUnit 4 rule dependency:Annotate a field in your test class with
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-junit-rule-no-dependencies</artifactId>
<version>5.15.0</version>
</dependency>
@Rule:@Rule
public MockServerRule mockServerRule = new MockServerRule(this);
private MockServerClient mockServerClient;
MockServerRule starts MockServer on a free port before any test runs and stops it after all tests complete. An instance of MockServerClient is automatically assigned to any field of type org.mockserver.client.MockServerClient.Constructor options// Dynamically allocate a free port
public MockServerRule(Object target);
// Control whether one instance is shared per JVM or per test class
public MockServerRule(Object target, boolean perTestSuite);
// Bind to specific port(s)
public MockServerRule(Object target, Integer... ports);
Add the JUnit 5 extension dependency:Annotate your test class with MockServer starts before any test runs and stops after all tests complete. A
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-junit-jupiter-no-dependencies</artifactId>
<version>5.15.0</version>
</dependency>
@ExtendWith(MockServerExtension.class):@ExtendWith(MockServerExtension.class)
class ExampleTestClass {
private final ClientAndServer client;
public ExampleTestClass(ClientAndServer client) {
this.client = client;
}
@Test
void testSomething() {
// use client to create expectations and verify requests
}
}
MockServerClient or ClientAndServer instance is injected via JUnit 5 parameter resolution into constructors, @BeforeEach/@BeforeAll methods, and test methods.Bind to specific portsUse @MockServerSettings to control which ports MockServer binds to:@ExtendWith(MockServerExtension.class)
@MockServerSettings(ports = {8787, 8888})
class ExampleTestClass {
private final ClientAndServer client;
public ExampleTestClass(ClientAndServer client) {
this.client = client;
}
@Test
void testSomething() {
// ...
}
}
Add the Spring test listener dependency:Annotate your test class with The Inject the port directly
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-spring-test-listener-no-dependencies</artifactId>
<version>5.15.0</version>
</dependency>
@MockServerTest:@MockServerTest
@RunWith(SpringRunner.class)
public class ExampleSpringTest {
private MockServerClient mockServerClient;
@Test
public void testSomething() {
// use mockServerClient to create expectations and verify requests
}
}
mockserver-spring-test-listener registers a Spring TestExecutionListener that starts MockServer on a free port when the test class is annotated with @MockServerTest. MockServer resets after each test and closes via a shutdown hook after all tests complete.Inject the port into Spring propertiesUse the ${mockServerPort} placeholder to configure a Spring bean with the MockServer URL:@RunWith(SpringRunner.class)
@MockServerTest("server.url=http://localhost:${mockServerPort}/path")
@ContextConfiguration(classes = MyTestConfig.class)
public class ExampleSpringTestWithUrl {
@Autowired
private MyClient client;
private MockServerClient mockServerClient;
@Test
public void testSomething() {
// ...
}
}
@MockServerTest
@RunWith(SpringRunner.class)
public class ExampleSpringTestWithPort {
@MockServerPort
Integer mockServerPort;
@Test
public void testSomething() {
// ...
}
}
@MockServerTest does not support parallel test execution because the MockServer instance is shared across all test classes. To run tests in parallel, use a unique value in each request matcher to avoid conflicts.