Stub WebSocket server in Java

1k Views Asked by At

What I am trying to achieve is to test some logic that has a web socket communication behind.

I have recently implemented WebSocket client endpoint and I wanted to test it like http integrations with WireMock - declaratively specifying web socket messages that are expected from external WebSocket server.

Expected library/API:

//      SomeLibrary is configured properly

    SomeLibrary.stubSocketMessage(forSession(idFromString("Some session id.")))
        .respondTo("Message sent from service with web socket client endpoint.")
        .withText("Response from web socket server stub.")
        .withDelay(500);

//        calling my logic that uses WS / sends message
//        assertions

I couldn't find such a library therefore I have simply implemented embedded websocket server in my test packages. It was sufficient for single session scenario.

To sum up: I have a websocket connection somewhere in my service layer and I would like to write end to end test with i.e. MockMvc. I want some predefined mock behaviour of WS so I need an embedded http/ws server and to specify behaviour of its WS server endpoint.

Is there solution for what I wanted to achieve? Does it make sense to write such a test? If not, why?

1

There are 1 best solutions below

0
On

@patrolfr,

I will try to answer a part of your question: if there is a solution.

You can have a look at https://stubby4j.com stub server. It supports stubbing external systems over HTTP/1.1, HTTP/2 and WebSockets protocols. The library uses a YAML-based configuration to describe the stub server behavior.

More specifically regarding the WebSockets protocol, please check the docs: https://stubby4j.com/docs/websockets_configuration_howto.html

Full disclosure: I am a committer to the project.