Self-Connect to Websocket Spring server from JUnit test

Askar Ibragimov picture Askar Ibragimov · Feb 3, 2017 · Viewed 7.6k times · Source

I have an app that exposes Websocket/SockJS/Stomp server endpoints and would like to run a JUnit tests that runs client (Java STOMP client, also from Spring) against it, to test "sending" features.

I have a test like

 public void measureSpeedWithWebsocket() throws Exception {
    final Waiter subscribeWaiter = new Waiter();

    new Thread(() -> {
        // Prepare connection
        WebsocketClient listener = new WebsocketClient("/mytopic/stomp");
        try {
            listener.connectAndSubscribe();
            subscribeWaiter.resume();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }).start();
    subscribeWaiter.await(); // Wait for connection.

Here I made use of Waiter from https://github.com/jhalterman/concurrentunit, which effect is basically to delay main thread of the test till secondary thread call resume(). This is likely wrong, because Spring server that is running in the context has to react

I am getting the following error

[INFO ] 2017-02-03 12:36:12.402 [Thread-19] WebsocketClient - Listening  
[INFO ] 2017-02-03 12:36:12.403 [Thread-19] WebsocketClient - Connecting to ws://localhost:8083/user...
2017-02-03 12:36:14.097 ERROR 9956 --- [      Thread-19] o.s.w.socket.sockjs.client.SockJsClient  : Initial SockJS "Info" request to server failed, url=ws://localhost:8083/user
    org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:8083/user/info": Connection refused: connect; nested exception is java.net.ConnectException: Connection refused: connect
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:633) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE]
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:595) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE]
    at org.springframework.web.socket.sockjs.client.RestTemplateXhrTransport.executeInfoRequestInternal(RestTemplateXhrTransport.java:138) ~[spring-websocket-4.3.3.RELEASE.jar:4.3.3.RELEASE]
    at org.springframework.web.socket.sockjs.client.AbstractXhrTransport.executeInfoRequest(AbstractXhrTransport.java:155) ~[spring-websocket-4.3.3.RELEASE.jar:4.3.3.RELEASE]
    at org.springframework.web.socket.sockjs.client.SockJsClient.getServerInfo(SockJsClient.java:286) ~[spring-websocket-4.3.3.RELEASE.jar:4.3.3.RELEASE]
    at org.springframework.web.socket.sockjs.client.SockJsClient.doHandshake(SockJsClient.java:254) ~[spring-websocket-4.3.3.RELEASE.jar:4.3.3.RELEASE]
    at org.springframework.web.socket.messaging.WebSocketStompClient.connect(WebSocketStompClient.java:274) [spring-websocket-4.3.3.RELEASE.jar:4.3.3.RELEASE]
    at org.springframework.web.socket.messaging.WebSocketStompClient.connect(WebSocketStompClient.java:255) [spring-websocket-4.3.3.RELEASE.jar:4.3.3.RELEASE]
    (...)
    at java.lang.Thread.run(Thread.java:745) ~[na:1.8.0_74]
Caused by: java.net.ConnectException: Connection refused: connect

How I can possibly make a proper test that "self-connects" to the websocket offered by my Spring Boot application?

Answer

Elka picture Elka · Mar 9, 2017

If you are using Spring Boot than you are a lucky one :). Here is an example http://rafaelhz.github.io/testing-websockets/ how you can test the web sockets, which perfectly works in Spring Boot and can helps you a lot. I am trying to do the same in Spring MVC but unfortunately that doesn't work in Spring MVC.