3

I need to mock java.net.Socket class using wiremock technique... Is it possible to wiremock a Socket class?

0

1 Answer 1

7

Even if I am bit too late, but in case this question is raised later by somebody:

I was exactly in the same situation. I implemented a separate JUnit @Rule which starts/stops the ServerSocket and accepts Socket requests from test cases.

I'm not allowed to share the code with you because it belongs to the company. But I'm allowed to share the powerslide which will surely help you how to achieve it.

enter image description here

In JUnit you are using a client/service that establish a socket communication to the given port (in my case: 9999). All WireMock responses for sockets are written in same style like HTTP requests/responses. Simplified example with "any" method:

WireMock.givenThat(WireMock.anyUrl()).withRequestBody(WireMock.containing("blah blah")).willReturn(WireMock.aResponse().withBody("Hello World!")));

In case your socket/client is sending the body with "blah blah" then WireMock will return "Hello World!" as HTTP Entity and the content of HTTP Entity is forwarded to the socket/client by Socket Proxy.

Hope it helps, Chris

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.