Spring Websocket Endpoints Testing

For the past week I’ve been playing around with Spring Websockets. I’ve used Spring Boot to get everything up and running, and after unit and Integration testing everything including my controllers, the only thing I didn’t test were my Websocket Endpoints.

Thinking it was a good idea to test these too, after the better part of a week I finally figured out how to do that. As I couldn’t find any examples out there tackling this particular problem I thought this might be a reference for people wanting to test their websocket endpoints.

I’ve created an example project with 2 endpoints and a very simple backend which always returns a GameState bean.


In the Spring @Configuration class I’ve added the following:

config.enableSimpleBroker(“/topic”); config.setApplicationDestinationPrefixes(“/app”);

This means I have to test these endpoints:


Test Class

First of all we need to setup our test to run the application. We do this by adding the following annotations to our test.

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

With the @SpringBootTest annotation the application will run and be configured to a random port.

After that we setup a Websocket Client to communicate to the Websocket server. We use the WebSocketStompClient from Spring to do this.

WebSocketStompClient stompClient = new WebSocketStompClient(new SockJsClient(createTransportClient()));
stompClient.setMessageConverter(new MappingJackson2MessageConverter());

StompSession stompSession = stompClient.connect(URL, new StompSessionHandlerAdapter() {
}).get(1, SECONDS);

Here we’ve added a JacksonMessageConverter to the WebsocketStompClient, and we connect to the designated URL.

We first subscribe to the create game endpoint and send a message to it. The payload we sent is null, otherwise we need a MessageConverter for it. For some reason if I also add a StringMessageConverter my returning GameState it will be wrongfully converted with the StringMessageConverter instead of the JacksonMessageConverter.

stompSession.subscribe(SUBSCRIBE_CREATE_BOARD_ENDPOINT + uuid, new CreateGameStompFrameHandler());
stompSession.send(SEND_CREATE_BOARD_ENDPOINT + uuid, null);

This will subscribe to the /game/topic/board/{uuid} endpoint, where the {uuid} is replaced by an actual UUID. Below you can see how I’ve implemented the CreateGameStompFrameHandler().

private class CreateGameStompFrameHandler implements StompFrameHandler {
public Type getPayloadType(StompHeaders stompHeaders) {
return GameState.class;

public void handleFrame(StompHeaders stompHeaders, Object o) {
completableFuture.complete((GameState) o);

In the getPayloadType(StompHeaders stompHeaders) we can declare which type the payload (content) will be converted to. This is where my problem lied, I was busy for days trying to get this Handler working. The problem was, it did call getPayloadType() but after that there was an exception about the payload not being able to be converted to the Type I declared, for some reason this exception doesn’t get written to the console. The same happens when you get an exception from the JacksonMapper because a default constructor can’t be found. These errors don’t get written to the console. When you also have this problem it might be beneficial to debug the AbstractMessageConverter or one of its derived classes.

When it works, the handleFrame(StompHeaders stompHeaders, Object payload) will get called and you can convert the Object to the type you want. In my test I call the CompletableFuture.complete to let it know I received a response and the test can be completed.

After we send a request to /game/app/create/{uuid} all the above gets called.

With all this I’ve created the following test.

Complete Test Class

I hope this helps someone looking to test there Websocket endpoints, if you any advice please let me know.


Source code for this can be found on Github: https://github.com/MBlokhuijzen/Spring-Websockets-IntegrationTest