Before the MockServer Proxy can record any requests it must be started.

The MockServer and MockServer Proxy can be run:

MockServer and MockServer Proxy is available as:

It is also possible to build and run MockServer directly from source code

To simplify configuration all MockServer versions (except the deployable WAR) only use a single port to support HTTP, HTTPS and SOCKS. This is achieved by dynamically detecting if the client is using HTTP, HTTPS or SOCKS.

Note: Also see the section on Configuring Clients to how to configure any of the following clients to use the MockServer Proxy:

 

Maven Plugin

To run MockServer as part of your build add the following plugin to your pom.xml:

<plugin>
    <groupId>org.mock-server</groupId>
    <artifactId>mockserver-maven-plugin</artifactId>
    <version>4.0.0</version>
    <configuration>
        <serverPort>1080</serverPort>
        <proxyPort>1090</proxyPort>
        <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>

This will start MockServer during the process-test-classes phase and will stop MockServer during the verify phase. For more details about Maven build phases see: Introduction to the Build Lifecycle.

This ensures that any integration tests you run during the test or integration-test phases can use MockServer on the port specified.

It is also possible to run MockServer as a forked JVM using the runForked and stopForked goals as follows:

 <plugin>
     <groupId>org.mock-server</groupId>
     <artifactId>mockserver-maven-plugin</artifactId>
     <version>4.0.0</version>
     <configuration>
        <serverPort>1080</serverPort>
        <proxyPort>1090</proxyPort>
        <logLevel>DEBUG</logLevel>
        <initializationClass>org.mockserver.maven.ExampleInitializationClass</initializationClass>
     </configuration>
     <executions>
         <execution>
             <id>process-test-classes</id>
             <phase>process-test-classes</phase>
             <goals>
                 <goal>runForked</goal>
             </goals>
         </execution>
         <execution>
             <id>verify</id>
             <phase>verify</phase>
             <goals>
                 <goal>stopForked</goal>
             </goals>
         </execution>
     </executions>
 </plugin>

Stop MockServer Even When Tests Fail

If you use the runForked goal as above and the test phase fails (because a test has failed) MockServer will not be stopped as Maven does not run any more phases after a phase has failed. In the case above the verify phase is not run if a test fails so the forked MockServer will not be stopped.

If you want to ensure MockServer is stopped even when there are test failures make sure you use start and stop goals as these run MockServer on a separate thread that is stopped however maven exits (even if a test fails).

Alternatively a TestListener can be used with maven-surefire-plugin to ensure that MockServer is stopped even when a test fails, as follows:

 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.17</version>
    <configuration>
        <properties>
            <property>
                <name>listener</name>
                <value>org.mockserver.maven.StopMockServerTestListener</value>
            </property>
        </properties>
    </configuration>
</plugin>

The Maven plugin can also be used from the command line to start and stop MockServer, as follows:

To run MockServer synchronously and block:

mvn mockserver:run

To run MockServer asynchronously as a forked JVM:

mvn mockserver:runForked

To stop a forked instance of MockServer running on the same machine:

mvn mockserver:stopForked

The stopForked goal does assumes that MockServer is running on the same physical machine as it uses 127.0.0.1 to communicate with MockServer stop socket.

The Maven plugin has the following goals:

  • start - start MockServer, do not block, but stop when build ends
  • stop - stop a MockServer started earlier as part of the current build
  • run - run MockServer and block waiting for requests (timeout config if provided limits how long to block for)
  • runForked - run MockServer as separate forked JVM, do not block, stay alive when build ends
  • stopForked - stop a forked MockServer, previously started by a runForked goal

The Maven plugin can be configured with the following properties:

  • serverPort - The port the MockServer listens for incoming request. Port unification is used to support both HTTP and HTTPS on the same port (required: false - but at least one of the two ports (serverPort or proxyPort) must be specified)
  • proxyPort - The port the proxy listens to incoming requests. Port unification is used to support all protocols on the same port (required: false - but at least one of the two ports (serverPort or proxyPort) must be specified)
  • timeout - How long to block waiting for MockServer, after the timeout the plugin will shutdown MockServer, used by run goal, 0 means wait indefinitely (required: false, default: 0)
  • logLevel - The logging level (required: false, default: WARN)
  • skip - Prevent the plugin from running (required: false, default: false)
  • initializationClass - To enable the creation of default expectations a class can be specified to initialize expectations in MockServer, this class must implement org.mockserver.initialize.ExpectationInitializer interface. The initializeExpectations(MockServerClient mockServerClient) method will be called once MockServer has been started (but ONLY if serverPort has been set). Note: that the plugin must be started during the process-test-classes to ensure that the initialization class has been compiled from either src/main/java or src/test/java locations. In addition the initializer can only be used with start and run goals, it will not work with the runForked goal as a JVM is forked with a separate classpath. (required: false, default: false)
 

Client API  -  starting and stopping

Use the client API to run MockServer programmatically.

First add the following maven dependency:

<!-- mockserver -->
<dependency>
     <groupId>org.mock-server</groupId>
     <artifactId>mockserver-netty</artifactId>
     <version>4.0.0</version>
</dependency>

To start the server and create a client the simplest way is to the start factory methods ClientAndServer.startClientAndServer or ClientAndProxy.startClientAndProxy as follows:

Add includes:

import static org.mockserver.integration.ClientAndProxy.startClientAndProxy;
import static org.mockserver.integration.ClientAndServer.startClientAndServer;

Add fields:

private ClientAndProxy proxy;
private ClientAndServer mockServer;

Use factory method to start server and client when appropriate, for example in @Before method:

@Before
public void startProxy() {
    mockServer = startClientAndServer(1080);
    proxy = startClientAndProxy(1090);
}

Stop server and client when appropriate, for example in @After method:

@After
public void stopProxy() {
    proxy.stop();
    mockServer.stop();
}

The mockserver-example project contains an example test called BookPageIntegrationTest that demonstrates a fully working example.

 

Running MockServer via a JUnit @Rule

MockServer can be run using the MockServerRule. The MockServerRule starts MockServer on a free port before the any test runs and stops MockServer after all tests have completed. A MockServerClient is then assigned to any field in the unit test of type org.mockserver.client.server.MockServerClient

@Rule
public MockServerRule mockServerRule = new MockServerRule(this);

private MockServerClient mockServerClient;

Any test method can now use the mockServerClient field to create expectation or verify requests.

The MockServerRule has the following constructors:

/**
 * Start MockServer prior to test execution and stop MockServer after the tests have completed.
 * This constructor dynamically allocates a free port for MockServer to use.
 *
 * @param target an instance of the test being executed
 */
public MockServerRule(Object target);

/**
 * Start MockServer prior to test execution and stop MockServer after the tests have completed.
 * This constructor dynamically allocates a free port for MockServer to use.
 *
 * @param target an instance of the test being executed
 * @param perTestSuite indicates how many instances of MockServer are created
 *                     if true a single MockServer is created per JVM
 *                     if false one instance per test class is created
 */
public MockServerRule(Object target, boolean per TestSuite);
/**
 * Start the proxy prior to test execution and stop the proxy after the tests have completed.
 * This constructor dynamically create a proxy that accepts HTTP(S) requests on the specified port
 *
 * @param port the HTTP(S) port for the proxy
 * @param target an instance of the test being executed
 */
public MockServerRule(Integer port, Object target);

/**
 * Start the proxy prior to test execution and stop the proxy after the tests have completed.
 * This constructor dynamically create a proxy that accepts HTTP(S) requests on the specified port
 *
 * @param port the HTTP(S) port for the proxy
 * @param httpsPort the HTTPS port for the proxy
 * @param target an instance of the test being executed
 * @param perTestSuite indicates how many instances of MockServer are created
 *                     if true a single MockServer is created per JVM
 *                     if false one instance per test class is created
 */
public MockServerRule(Integer port, Object target, boolean per TestSuite);

The proxy can be run using the ProxyRule. The ProxyRule starts the proxy before the any test runs and stops the proxy after all tests have completed. A ProxyClient is then assigned to any field in the unit test of type org.mockserver.client.proxy.ProxyClient

@Rule
public ProxyRule proxyRule = new ProxyRule(1080, this);

private ProxyClient proxyClient;

Any test method can now use the proxyClient field to verify requests or anaylse a system's behaviour.

The ProxyRule has the following constructors:

/**
 * Start the proxy prior to test execution and stop the proxy after the tests have completed.
 * This constructor dynamically allocates a free port for the proxy to use.
 * Note: The getHttpPort getter can be used to retrieve the dynamically allocated port.
 *
 * @param target an instance of the test being executed
 */
public ProxyRule(Object target);

/**
 * Start the proxy prior to test execution and stop the proxy after the tests have completed.
 * This constructor dynamically allocates a free port for the proxy to use.
 *
 * @param target an instance of the test being executed
 * @param perTestSuite indicates how many instances of the proxy are created
 *                     if true a single proxy is created per JVM
 *                     if false one instance per test class is created
 */
public ProxyRule(Object target, boolean per TestSuite);

/**
 * Start the proxy prior to test execution and stop the proxy after the tests have completed.
 * This constructor dynamically create a proxy that accepts HTTP(S) requests on the specified port
 *
 * @param port the HTTP(S) port for the proxy
 * @param target an instance of the test being executed
 */
public ProxyRule(Integer port, Object target);

/**
 * Start the proxy prior to test execution and stop the proxy after the tests have completed.
 * This constructor dynamically create a proxy that accepts HTTP(S) requests on the specified port
 *
 * @param port the HTTP(S) port for the proxy
 * @param httpsPort the HTTPS port for the proxy
 * @param target an instance of the test being executed
 * @param perTestSuite indicates how many instances of the proxy are created
 *                     if true a single proxy is created per JVM
 *                     if false one instance per test class is created
 */
public ProxyRule(Integer port, Object target, boolean per TestSuite);
 

Running From Command Line

MockServer can be run from the command line in the following ways:

 

Running From Command Line - Using Homebrew

Homebrew, a packaging manager for OS X (i.e. Apple Mac), can be used to install MockServer, as follows:

brew update && brew install mockserver

The MockServer formula in Homebrew performs the following actions:

  1. installed the binaries and scripts
  2. creates the log directory
  3. add the scripts to the PATH variable

Once the MockServer has been installed by Homebrew it is available from any command shell as the mockserver command

The mockserver command supports the following options:

mockserver [-logLevel <level>]  [-serverPort <port>]  [-proxyPort <port>]  [-proxyRemotePort <port>]  [-proxyRemoteHost <hostname>]

 valid options are:
    -logLevel <level>            OFF, ERROR, WARN, INFO, DEBUG, TRACE or ALL, as follows:
                                 WARN - exceptions and errors
                                 INFO - all interactions

    -serverPort <port>           Specifies the HTTP, HTTPS, SOCKS and HTTP
                                 CONNECT port for proxy. Port unification
                                 supports for all protocols on the same port.

    -proxyPort <port>            Specifies the HTTP and HTTPS port for the
                                 MockServer. Port unification is used to
                                 support HTTP and HTTPS on the same port.

    -proxyRemotePort <port>      Specifies the port to forward all proxy
                                 requests to (i.e. all requests received on
                                 portPort). This setting is used to enable
                                 the port forwarding mode therefore this
                                 option disables the HTTP, HTTPS, SOCKS and
                                 HTTP CONNECT support.

    -proxyRemoteHost <hostname>  Specified the host to forward all proxy
                                 requests to (i.e. all requests received on
                                 portPort). This setting is ignored unless
                                 proxyRemotePort has been specified. If no
                                 value is provided for proxyRemoteHost when
                                 proxyRemotePort has been specified,
                                 proxyRemoteHost will default to "localhost".

i.e. mockserver -logLevel INFO -serverPort 1080 -proxyPort 1090 -proxyRemotePort 80 -proxyRemoteHost www.mock-server.com

For example run the MockServer, as follows:

mockserver -logLevel INFO -serverPort 1080

Or run the MockServer Proxy, as follows:

mockserver -logLevel INFO -proxyPort 1090

Logging

When MockServer is installed via Homebrew a log directory is created at /usr/local/var/log/mockserver. All logs are written into this directory, this includes normal application logs, which are are written in /usr/local/var/log/mockserver/mockserver.log, and request logs, which are written into /usr/local/var/log/mockserver/mockserver_request.log (when a dumpToLog request is received).

The amount of logs written into the application log /usr/local/var/log/mockserver/mockserver.log can be controlled using the -logLevel command as detailed above.

 

Running From Command Line - Using Java

MockServer can be run directly from the command line using java directly as follow:

  1. download mockserver-netty-4.0.0-jar-with-dependencies.jar from Maven Central

  2. java -jar <path to mockserver-netty-4.0.0-jar-with-dependencies.jar> -serverPort <port>

The command line supports the following options:

java -jar <path to mockserver-jetty-jar-with-dependencies.jar> [-serverPort <port>] \
                                                               [-proxyPort <port>] \
                                                               [-proxyRemotePort <port>] \
                                                               [-proxyRemoteHost <hostname>]

 valid options are:
    -serverPort <port>           Specifies the HTTP, HTTPS, SOCKS and HTTP
                                 CONNECT port for proxy. Port unification
                                 supports for all protocols on the same port.

    -proxyPort <port>            Specifies the HTTP and HTTPS port for the
                                 MockServer. Port unification is used to
                                 support HTTP and HTTPS on the same port.

    -proxyRemotePort <port>      Specifies the port to forward all proxy
                                 requests to (i.e. all requests received on
                                 portPort). This setting is used to enable
                                 the port forwarding mode therefore this
                                 option disables the HTTP, HTTPS, SOCKS and
                                 HTTP CONNECT support.

    -proxyRemoteHost <hostname>  Specified the host to forward all proxy
                                 requests to (i.e. all requests received on
                                 portPort). This setting is ignored unless
                                 proxyRemotePort has been specified. If no
                                 value is provided for proxyRemoteHost when
                                 proxyRemotePort has been specified,
                                 proxyRemoteHost will default to "localhost".

i.e. java -jar ./mockserver-jetty-jar-with-dependencies.jar -serverPort 1080 -proxyPort 1090 -proxyRemotePort 80 -proxyRemoteHost www.mock-server.com

For example:

java -Dmockserver.logLevel=INFO -jar ~/Downloads/mockserver-netty-4.0.0-jar-with-dependencies.jar -serverPort 1080 -proxyPort 1090

All interactions with the MockServer are logged including setting up expectations, matching expectations, clearing expectations and verifying requests. The when running from the command line, Maven plugin, npm module or Grunt plugin the log is written to a file called mockserver.log in the current working directory where the MockServer is running. This log can be particularly helpful when trying to debug why a test is failing or expectations are not being matched.

The system property mockserver.logLevel can be used to set the log level, as shown above.

It is also possible to specify a custom logback configuration file to override the default MockServer or MockServer Proxy logging settings. An example logback configuration file is available in github.

A custom logback configuration file can be specified using the logback.configurationFile system property with an absolute or relative file path or a classpath, as follows:

java -Droot.logLevel=WARN -Dmockserver.logLevel=INFO -Dlogback.configurationFile=example_logback.xml -jar ~/Downloads/mockserver-netty-4.0.0-jar-with-dependencies.jar -serverPort 1080 -proxyPort 1090

A custom logback configuration file will also be automatically picked up if it is called logback.xml and is in the root of the classpath, however, the jar-with-dependencies already contains a logback.xml file, so to override this, the overriding logback.xml file must be higher (i.e. earlier) in the classpath.

Disabling Logging

To disable logging the following options can be used:

  • -Dmockserver.logLevel=OFF - to disable logging from the MockServer and MockServer Proxy classes
  • -Droot.logLevel=OFF - to disable all logging from all other classes (i.e. all none MockServer and MockServer Proxy classes)

If logging is disabled then no log file will be created. This is because the log files are only created when the first item is written to the log file.

 

Running From Command Line - Using Maven Plugin

MockServer can be run directly from the command line and using the mockserver-maven-plugin as follow:

mvn -Dmockserver.serverPort=1080 -Dmockserver.proxyPort=1090 -Dmockserver.logLevel=INFO org.mock-server:mockserver-maven-plugin:4.0.0:runForked

When run from the command line the Maven plugin can be configured with the following properties:

  • mockserver.serverPort - The port the MockServer listens for incoming request. Port unification is used to support both HTTP and HTTPS on the same port (required: false - but at least one of the two ports (serverPort or proxyPort) must be specified)
  • mockserver.proxyPort - The port the proxy listens to incoming requests. Port unification is used to support all protocols on the same port (required: false - but at least one of the two ports (serverPort or proxyPort) must be specified)
  • mockserver.logLevel - The logging level (required: false, default: WARN)

The runForked goal of the mockserver-maven-plugin will fork a JVM process containing the Netty based MockServer. To stop the forked JVM process use the stopForked goal, as follows:

mvn -Dmockserver.serverPort=1080 -Dmockserver.proxyPort=1090 org.mock-server:mockserver-maven-plugin:4.0.0:stopForked

For more information on the mockserver-maven-plugin see the section on MockServer Maven Plugin

 

Web Archive (WAR)

To run as a WAR deployed on any JEE web server:

  1. download mockserver-war-4.0.0.war from Maven Central
  2. deploy mockserver-war-4.0.0.war to any JEE web server

WAR Context Path

The WAR context path is ignored from all request matching for path.

The MockServerClient constructor includes an argument for the context path that the WAR has been deployed to, as follows:

public MockServerClient(String host, int port, String contextPath)
 

NPM Module & MockServer Grunt Plugin

The node module can be used to start and stop MockServer and the MockServer proxy as a node module or as a Grunt plugin.

You may install this plugin / node module with the following command:

npm install mockserver-node --save-dev

Node Module

To start or stop the MockServer from any Node.js code you need to import this module using require('mockserver-node') as follows:

var mockserver = require('mockserver-node');

Then you can use either the start_mockserver or stop_mockserver functions as follows:

var mockserver = require('mockserver-node');
mockserver.start_mockserver({
                serverPort: 1080,
                proxyPort: 1090,
                verbose: true
            });

// do something

mockserver.stop_mockserver();

If you are only using the MockServer then only specify the MockServer port as follows:

mockserver.start_mockserver({serverPort: 1080});

// do something

mockserver.stop_mockserver();

The MockServer and the MockServer proxy use port unification to support HTTP and HTTPS on the same port. A client can then connect to the single port with both HTTP and HTTPS as the socket will automatically detected SSL traffic and decrypt it when required.

Grunt Plugin

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins.

In your project's Gruntfile, add a section named start_mockserver and stop_mockserver to the data object passed into grunt.initConfig().

The following example will result in a both a MockServer and a MockServer proxy being started on ports 1080 and 1090.

grunt.initConfig({
    start_mockserver: {
        start: {
            options: {
                serverPort: 1080,
                proxyPort: 1090
            }
        }
    },
    stop_mockserver: {
        stop: {

        }
    }
});

grunt.loadNpmTasks('mockserver-node');

Grunt Plugin & NPM Module Options

options.serverPort

Type: Integer Default: undefined

This value specifies the HTTP and HTTPS port for the MockServer port unification is used to support HTTP and HTTPS on the same port. The MockServer will only be started if a port is provided, if this value is left undefined the MockServer will not be started.

options.proxyPort

Type: Integer Default: undefined

This value specifies the HTTP, HTTPS, SOCKS and HTTP CONNECT port for proxy, port unification is used to support all protocols on the same port. The proxy will only be started if a port is provided, if this value is left undefined the proxy will not be started.

options.verbose

Type: Boolean Default: false

This value indicates whether the MockServer logs should be written to the console. In addition to logging additional output from the grunt task this options also sets the logging level of the MockServer to INFO. At INFO level all interactions with the MockServer including setting up expectations, matching expectations, clearing expectations and verifying requests are written to the log. The MockServer logs are written to mockserver.log in the current directory.

Note: It is also possible to use the --verbose command line switch to enabled verbose level logging from the command line.

options.trace

Type: Boolean Default: false

This value sets the logging level of the MockServer to TRACE. At TRACE level (in addition to INFO level information) all matcher results, including when specific matchers fail (such as HeaderMatcher) are written to the log. The MockServer logs are written to mockserver.log in the current directory.

options.javaDebugPort

Type: Integer Default: undefined

This value indicates whether Java debugging should be enabled and if so which port the debugger should listen on. When this options is provided the following additional option is passed to the JVM:

"-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=" + javaDebugPort

Note: suspend=y is used so the MockServer will pause until the debugger is attached. The grunt task will wait 50 seconds for the debugger to be attached before it exits with a failure status.

 

Docker Container

The typical sequence for running the MockServer docker image is as follows:
  1. Install Docker
  2. Pull (or Update) Image
  3. Run Container
In addition it is possible to customise how the container is run.  

Install Docker

To install Docker see the installation instructions.

 

Pull MockServer Image

To pull the MockServer Docker image use the pull command, as follows:

docker pull jamesdbloom/mockserver

This is not strictly necessary as the image will be automatically pulled if it does not exist when the run command is used. However, using the pull command will ensure the latest version of the image is downloaded.

 

Run MockServer Container

Then to run MockServer as a Docker container run the following command:

docker run -d -P jamesdbloom/mockserver

The -P switch in this command tells Docker to map all ports exported by the MockServer container to dynamically allocated ports on the host machine.

To view information about the MockServer container, including which dynamic ports have been used run the following command:

docker ps
 

Configure Port Mapping

This MockServer docker container exports the following ports:

  • serverPort 1080
  • proxyPort 9090

To specify which ports (on the host machine) should be mapped to the MockServer docker container use the -p <host port>:<container port> option, as follows:

docker run -d -p <serverPort>:1080 -p <proxyPort>:1090 jamesdbloom/mockserver

For example:

docker run -d -p 1080:1080 -p 1090:1090 jamesdbloom/mockserver

Only specify the required ports, for example, if you are not using the proxy there is no need to provide port mapping options for the proxy ports, as follows:

docker run -d -p 1080:1080 jamesdbloom/mockserver

Modifying Default Command

By default when the MockServer container runs it executes a bash script passing three command line options, as follows

/opt/mockserver/run_mockserver.sh -logLevel INFO -serverPort 1080 -proxyPort 1090

It is possible to pass an alternative command line to the container, by pre-pending the command to the end of the run command, as follows:

docker run -d -p 1080:1080 -p 1090:1090 jamesdbloom/mockserver /opt/mockserver/run_mockserver.sh -logLevel INFO -serverPort
	1080 -proxyPort 1090

For following command can be used to view the available command line switches:

docker run jamesdbloom/mockserver /opt/mockserver/run_mockserver.sh

   Error: At least 'serverPort' or 'proxyPort' must be provided

   run_mockserver.sh [-logLevel <level>] \
                     [-serverPort <port>] \
                     [-proxyPort <port>] \
                     [-proxyRemotePort <port>] \
                     [-proxyRemoteHost <hostname>]

     valid options are:
        -logLevel <level>                       OFF, ERROR, WARN, INFO, DEBUG, TRACE or ALL, as follows:
                                                WARN - exceptions and errors
                                                INFO - all interactions

        -serverPort <port>                      Specifies the HTTP, HTTPS, SOCKS and HTTP
                                                CONNECT port for proxy. Port unification
                                                supports for all protocols on the same port

        -proxyPort <port>                       Specifies the HTTP and HTTPS port for the
                                                MockServer. Port unification is used to
                                                support HTTP and HTTPS on the same port

        -proxyRemotePort <port>                 Specifies the port to forward all proxy
                                                requests to (i.e. all requests received on
                                                portPort). This setting is used to enable
                                                the port forwarding mode therefore this
                                                option disables the HTTP, HTTPS, SOCKS and
                                                HTTP CONNECT support

        -proxyRemoteHost <hostname>             Specified the host to forward all proxy
                                                requests to (i.e. all requests received on
                                                portPort). This setting is ignored unless
                                                proxyRemotePort has been specified. If no
                                                value is provided for proxyRemoteHost when
                                                proxyRemotePort has been specified,
                                                proxyRemoteHost will default to "localhost".

        -genericJVMOptions <system parameters>  Specified generic JVM options or system properties.

   i.e. /opt/mockserver/run_mockserver.sh -logLevel INFO -serverPort 1080 -proxyPort 1090 -proxyRemotePort 80 -proxyRemoteHost www.mock-server.com -genericJVMOptions "-Dmockserver.enableCORSForAllResponses=true -Dmockserver.sslSubjectAlternativeNameDomains='org.mock-server.com,mock-server.com'"

Then the appropriate options can be specified, for example, to setup a direct proxy (from 0.0.0.0:1090 to www.mock-server.com:80) using the following command:

docker run -d -p 1090:1090 jamesdbloom/mockserver /opt/mockserver/run_mockserver.sh -proxyPort 1090 -proxyRemotePort 80 -proxyRemoteHost www.mock-server.com

Interactive Shell

It is possible to launch the container with an interactive bash shell as follows:

docker run -it -p 1080:1080 -p 1090:1090 jamesdbloom/mockserver /bin/bash

Note: in this example above the -d flag (for daemon) has been replaced with -i (to stdin open) and -t (for pseudo-tty) to ensure docker creates the container in the foreground with an attached stdin, see the docker documentation for more details.

 

Vert.X Module

MockServer can be run as a Vert.X module. However, support for the Vert.X module was stopped at version 2.9. This is because Vert.X requires Java 7 and MockServer Vert.X module was the only MockServer module that relied on Java 7 making the build and development process more complex.

First, you'll need to install Vert.x.

Then to run MockServer as Vert.X module run the following command:

vertx install org.mock-server~mockserver-vertx~2.9
export VERTX_OPTS="-Dmockserver.serverPort=1080 -Dmockserver.serverSecurePort=1443 -Dmockserver.logLevel=TRACE"
vertx runmod org.mock-server~mockserver-vertx~2.9

The specify the HTTP port and / or HTTPS port the VERTX_OPTS environment variable must be set specifying the system property mockserver.serverPort for the HTTP port and mockserver.serverSecurePort for the HTTPS port.

Note: At least one of either the HTTP or the HTTPS port must be specified for MockServer to run.

export VERTX_OPTS="-Dmockserver.serverPort=1080 -Dmockserver.serverSecurePort=1443 -Dmockserver.logLevel=TRACE"

It is also possible to update the default logging level by setting the system properties mockserver.logLevel as above.

 

Build & Run From Source

MockServer is now only built using maven as the use of gradle caused problems with the Travis CI environment so the gradle build was removed.

First clone the repository as follows:

git clone https://github.com/jamesdbloom/mockservice.git
cd mockserver

Next use maven to build an executable jar containing all dependencies as follows:

mvn clean package

This will produce a jar file under the target directory called, as follows:

mockserver-netty/target/mockserver-netty-4.0.0-jar-with-dependencies.jar

Run MockServer then using the executable jar as per the instruction above in Running From The Command Line

Requirements

  • Java 6+   to use the Netty version, the Maven Plugin and MockServer WAR and MockServer Proxy WAR