MockServer supports the following features to simplify debugging

 

Retrieving Recorded Requests

All requests the MockServer or the proxy receives are recorded.

It is possible to retrieve the recorded request, as show below in the code examples.

Requests are returned in the order they are received, and which requests are returned can be filter using a request matcher.

HttpRequest[] recordedRequests = new MockServerClient("localhost", 1080)
    .retrieveRecordedRequests(
        request()
    );
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080)
    .retrieveRecordedRequests({})
    .then(
        function (recordedRequests) {
            console.log(JSON.stringify(recordedRequests));
        },
        function (error) {
            console.log(error);
        }
    );
curl -v -X PUT "http://localhost:1080/retrieve?type=REQUESTS"
HttpRequest[] recordedRequests = new MockServerClient("localhost", 1080)
    .retrieveRecordedRequests(
        request()
            .withPath("/some/path")
            .withMethod("POST")
    );
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080).retrieveRecordedRequests({
    "path": "/some/path",
    "method": "POST"
}).then(
    function (recordedRequests) {
        console.log(JSON.stringify(recordedRequests));
    },
    function (error) {
        console.log(error);
    }
);
curl -v -X PUT "http://localhost:1080/retrieve?type=REQUESTS" -d '{
    "path": "/some/path",
    "method": "POST"
}'
String recordedRequests = new MockServerClient("localhost", 1080)
    .retrieveRecordedRequests(
        request()
            .withPath("/some/path")
            .withMethod("POST"),
        Format.JAVA
    );
curl -v -X PUT "http://localhost:1080/retrieve?type=REQUESTS&format=JAVA" -d '{
    "path": "/some/path"
}'
String recordedRequests = new MockServerClient("localhost", 1080)
    .retrieveRecordedRequests(
        request()
            .withPath("/some/path")
            .withMethod("POST"),
        Format.JSON
    );
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080).retrieveRecordedRequests({
    "path": "/some/path",
    "method": "POST"
}).then(
    function (recordedRequests) {
        console.log(JSON.stringify(recordedRequests));
    },
    function (error) {
        console.log(error);
    }
);
curl -v -X PUT "http://localhost:1080/retrieve?type=REQUESTS&format=JSON" -d '{
    "path": "/some/path"
}'
 

Retrieving Active Expectations

It is possible to retrieve the active expectations, as show below in the code examples.

Expectations are returned in the order they have been added, and which expectations are returned can be filter using a request matcher.

Expectation[] activeExpectations = new MockServerClient("localhost", 1080)
    .retrieveActiveExpectations(
        request()
    );
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080)
    .retrieveActiveExpectations({})
    .then(
        function (activeExpectations) {
            console.log(JSON.stringify(activeExpectations));
        },
        function (error) {
            console.log(error);
        }
    );
curl -v -X PUT "http://localhost:1080/retrieve?type=ACTIVE_EXPECTATIONS"
Expectation[] activeExpectations = new MockServerClient("localhost", 1080)
    .retrieveActiveExpectations(
        request()
            .withPath("/some/path")
            .withMethod("POST")
    );
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080).retrieveActiveExpectations({
    "path": "/some/path",
    "method": "POST"
}).then(
    function (activeExpectations) {
        console.log(JSON.stringify(activeExpectations));
    },
    function (error) {
        console.log(error);
    }
);
curl -v -X PUT "http://localhost:1080/retrieve?type=ACTIVE_EXPECTATIONS" -d '{
    "path": "/some/path",
    "method": "POST"
}'
String activeExpectations = new MockServerClient("localhost", 1080)
    .retrieveActiveExpectations(
        request()
            .withPath("/some/path")
            .withMethod("POST"),
        Format.JAVA
    );
curl -v -X PUT "http://localhost:1080/retrieve?type=ACTIVE_EXPECTATIONS&format=JAVA" -d '{
    "path": "/some/path"
}'
String activeExpectations = new MockServerClient("localhost", 1080)
    .retrieveActiveExpectations(
        request()
            .withPath("/some/path")
            .withMethod("POST"),
        Format.JSON
    );
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080).retrieveActiveExpectations({
    "path": "/some/path",
    "method": "POST"
}).then(
    function (activeExpectations) {
        console.log(JSON.stringify(activeExpectations));
    },
    function (error) {
        console.log(error);
    }
);
curl -v -X PUT "http://localhost:1080/retrieve?type=ACTIVE_EXPECTATIONS&format=JSON" -d '{
    "path": "/some/path"
}'
 

Retrieving Recorded Expectations

For MockServer forward actions or all proxy requests expectations are recorded containing the request received and response returned.

It is possible to retrieve the recorded expectations, as show below in the code examples.

Expectations are returned in the order they have been recorded, and which expectations are returned can be filter using a request matcher.

Expectation[] recordedExpectations = new MockServerClient("localhost", 1080)
    .retrieveRecordedExpectations(
        request()
    );
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080)
    .retrieveRecordedExpectations({})
    .then(
        function (recordedExpectations) {
            console.log(JSON.stringify(recordedExpectations));
        },
        function (error) {
            console.log(error);
        }
    );
curl -v -X PUT "http://localhost:1080/retrieve?type=RECORDED_EXPECTATIONS"
Expectation[] recordedExpectations = new MockServerClient("localhost", 1080)
    .retrieveRecordedExpectations(
        request()
            .withPath("/some/path")
            .withMethod("POST")
    );
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080).retrieveRecordedExpectations({
    "path": "/some/path",
    "method": "POST"
}).then(
    function (recordedExpectations) {
        console.log(JSON.stringify(recordedExpectations));
    },
    function (error) {
        console.log(error);
    }
);
curl -v -X PUT "http://localhost:1080/retrieve?type=RECORDED_EXPECTATIONS" -d '{
    "path": "/some/path",
    "method": "POST"
}'
String recordedExpectations = new MockServerClient("localhost", 1080)
    .retrieveRecordedExpectations(
        request()
            .withPath("/some/path")
            .withMethod("POST"),
        Format.JAVA
    );
curl -v -X PUT "http://localhost:1080/retrieve?type=RECORDED_EXPECTATIONS&format=JAVA" -d '{
    "path": "/some/path"
}'
String recordedExpectations = new MockServerClient("localhost", 1080)
    .retrieveRecordedExpectations(
        request()
            .withPath("/some/path")
            .withMethod("POST"),
        Format.JSON
    );
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080).retrieveRecordedExpectations({
    "path": "/some/path",
    "method": "POST"
}).then(
    function (recordedExpectations) {
        console.log(JSON.stringify(recordedExpectations));
    },
    function (error) {
        console.log(error);
    }
);
curl -v -X PUT "http://localhost:1080/retrieve?type=RECORDED_EXPECTATIONS&format=JSON" -d '{
    "path": "/some/path"
}'
 

Retrieving Recorded Logs

MockServer and the proxy record log messages for all major actions, including:

It is possible to retrieve the recorded log messages, as show below in the code examples.

Log messages are returned in the order they have been recorded, and which log messages are returned can be filter using a request matcher.

String logMessages = new MockServerClient("localhost", 1080)
    .retrieveLogMessages(
        request()
    );
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080)
    .retrieveLogMessages({})
    .then(
        function (logMessages) {
            // logMessages is a String[]
            console.log(logMessages);
        },
        function (error) {
            console.log(error);
        }
    );
curl -v -X PUT "http://localhost:1080/retrieve?type=LOGS"
String logMessages = new MockServerClient("localhost", 1080)
    .retrieveLogMessages(
        request()
            .withPath("/some/path")
            .withMethod("POST")
    );
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080)
    .retrieveLogMessages({
        "path": "/some/path",
        "method": "POST"
    })
    .then(
        function (logMessages) {
            // logMessages is a String[]
            console.log(logMessages);
        },
        function (error) {
            console.log(error);
        }
    );
curl -v -X PUT "http://localhost:1080/retrieve?type=LOGS" -d '{
    "path": "/some/path",
    "method": "POST"
}'
String[] logMessages = new MockServerClient("localhost", 1080)
    .retrieveLogMessagesArray(
        request()
            .withPath("/some/path")
    );
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080)
    .retrieveLogMessages({
        "path": "/some/path"
    })
    .then(
        function (logMessages) {
            // logMessages is a String[]
            console.log(logMessages);
        },
        function (error) {
            console.log(error);
        }
    );
 

Logging

All interactions with the MockServer are logged including setting up expectations, matching expectations, clearing expectations and verifying requests. The log can be particularly helpful when trying to debug why a test is failing or expectations are not being matched.

The following information is logged:

  • WARN - exceptions, errors
  • INFO - all interactions with the MockServer including setting up expectations, matching expectations, clearing expectations and verifying requests
  • TRACE - all matcher results, including when specific matchers fail (such as HeaderMatcher)

The TRACE level logging results in a lot of verbose logging but can be very helpful to debug why a complex matcher (such as the JSON Schema matcher) is not matching.

When MockServer is run 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.

 

Logging with JUnit @Rule or API

If the MockSever is being launched using the JUnit @Rule or programmatically via an API then 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: -Dlogback.configurationFile=example_logback.xml

 

Logging with Command Line

When running MockServer directly from the command line the command line argument -logLevel can be used to set the log level, as follows:

java -jar ~/Downloads/mockserver-netty-5.4.1-jar-with-dependencies.jar -serverPort 1080 -logLevel DEBUG

Alternatively a system property mockserver.logLevel can be used to set the log level, as follows:

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

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-5.4.1-jar-with-dependencies.jar -serverPort 1080

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.

 

Logging with Maven Plugin

The mockserver-maven-plugin provides a logLevel settings that can be used to define the log level for all MockServer classes, as follows:

 <plugin>
     <groupId>org.mock-server</groupId>
     <artifactId>mockserver-maven-plugin</artifactId>
     <version>5.4.1</version>
     <configuration>
        <serverPort>1080</serverPort>
        <logLevel>DEBUG</logLevel>
     </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>
 

Logging with npm module or Grunt plugin

When running MockServer using the mockserver-node Grunt plugin and Node.js (npm) module the verbose option can be used to enable INFO level logging and the trace option can be used to enable TRACE level logging. In addition the --verbose command line flag can be used for Grunt builds to enable the mockserver-node verbose option dynamically.

 

Disabling Logging

To disable logging the following options can be used:

  • -Dmockserver.logLevel=OFF - to disable logging from the MockServer and Proxy classes
  • -Droot.logLevel=OFF - to disable all logging from all other 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.