javamockserver

How to fix SocketTimout execction on mockserver


I want to use mockserver to mock requests and responses. For that, I am using the mockserver docker image. Following is then the docker-compose file example where use mockserver.

mockServer:
  image: mockserver/mockserver
  container_name: mockhttpserver
  ports:
    - 1080:1080

Now in order to add expectations. I am using following mockserver java client.

<dependency>
  <groupId>org.mock-server</groupId>
  <artifactId>mockserver-client-java</artifactId>
  <version>5.11.2</version>
</dependency>

Following is my java code to create mockserverclient and add expectations. but when I am running this code it's giving me a SockedTimeout Exception. Don't know what's wrong with that. The Mockserver is running as a container and when I telnet to mylocalhost.org 1080 then it successfully returns connected but from eclipse, it throws exceptions.

MockServerClient client = new MockServerClient("mylocalhost.org", 1080);
        client.upsert(
                Expectation.when(HttpRequest.request("/login")
                .withMethod("get"))
                .thenRespond(HttpResponse.response().withStatusCode(200)));
        client.close();

Exception :

8:11:18.768 [MockServer-MockServerClient-eventLoop0] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.linkCapacity: 16
08:11:18.768 [MockServer-MockServerClient-eventLoop0] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.ratio: 8
Exception in thread "main" org.mockserver.client.SocketCommunicationException: Response was not received from MockServer after 20000 milliseconds, to wait longer please use "mockserver.maxSocketTimeout" system property or ConfigurationProperties.maxSocketTimeout(long milliseconds)
    at org.mockserver.client.NettyHttpClient.sendRequest(NettyHttpClient.java:172)
    at org.mockserver.client.MockServerClient.sendRequest(MockServerClient.java:211)
    at org.mockserver.client.MockServerClient.sendRequest(MockServerClient.java:244)
    at org.mockserver.client.MockServerClient.upsert(MockServerClient.java:1109)

But when I call the rest api to add expectation, it works.

curl -v -X PUT "http://mylocalhost.org:1080/mockserver/expectation"


Solution

  • Now its working. Following is the correct style to mock request and response.

    new MockServerClient("mylocalhost.org, 1080)
            .when(
                    HttpRequest.request()
                    .withMethod("GET")
                    .withPath("/view/cart")
            )
            .respond(
                    HttpResponse.response()
                    .withBody("some_response_body")
            );