9

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?

onkami
  • 8,791
  • 17
  • 90
  • 176
  • 1
    If you are using something like `@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)` in your test make sure that you actually use the correct port. The error message in your log suggests that you hardcoded the port into the URI (the number just looks like it ^^). You will get just that stacktrace of yours if the port is not correct. Spring provides means to discover the port, see [here](https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#howto-discover-the-http-port-at-runtime). – Fencer Dec 20 '17 at 12:11
  • Did the accepted answer work for you? I am getting same error as you got and i am following the same link as mentioned in the accepted answer, Did you do anything differently? – M.Ahsen Taqi Jul 13 '20 at 12:51

1 Answers1

2

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.

Elka
  • 227
  • 1
  • 9
  • it runs fine but when i replace that message with my request data to the websocket and expect the response that my websocket are sending to browser , then this code returns null and the assertion fails. I have posted question related to this here https://stackoverflow.com/questions/62870267/unit-test-for-websocket-returns-null can you please guide me what am i doing wrong? Thanks. – M.Ahsen Taqi Jul 13 '20 at 11:52