--- title: Running Tests In Parallel description: Run tests in parallel against a shared MockServer instance by using unique session IDs in request matchers to isolate each test. layout: page pageOrder: 8 section: 'Mock Server' subsection: true sitemap: priority: 0.7 changefreq: 'monthly' lastmod: 2019-11-10T08:00:00+01:00 ---

To ensure all tests can run in parallel with completely isolated independent data use a unique value for each test. The unique value for a test should be specified in each request matcher for all expectations for that test. This ensures that expectations created by one test are not visible by other tests and therefore keeps test isolated without needing multiple separate instances of MockServer.

For example, if each test generates a unique value (i.e. a UUID) for the sessionId cookie or a correlationId header then this header or cookie can be specified in each expectation's request matcher ensuring only requests with that unique value could be matched against expectations with that unique value.

Note: the drawback with this approach is that the logs will be more complex to understand because there will be a large number of expectation match failures, therefore it is recommended to either use a unique instance of MockServer per test or to run tests in series and clear all expectations prior to each test.

Java

    String sessionId = UUID.randomUUID().toString();

    new MockServerClient("127.0.0.1", 1080)
        .when(
            request()
                .withMethod("GET")
                .withPath("/somePath")
                .withCookies(
                    cookie("sessionId", sessionId)
                )
        )
        .respond(
            response()
                .withStatusCode(200)
                .withBody("{ name: 'value' }")
        );

JavaScript

    function guid() {
        function s4() {
            return Math.floor((1 + Math.random()) * 0x10000)
                .toString(16)
                .substring(1);
        }
        return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
            s4() + '-' + s4() + s4() + s4();
    }

    var sessionId = guid();

    mockServerClient("localhost", 1080)
        .mockAnyResponse({
            'httpRequest': {
                'method': 'GET',
                'path': '/somePath',
                "cookies": {
                    "sessionId": sessionId
                }
            },
            'httpResponse': {
                'statusCode': 200,
                'body': JSON.stringify({name: 'value'})
            }
        })
        .then(
            function () {
                console.log("expectation created");
            },
            function (error) {
                console.log(error);
            }
        );