title: Self-Connect to Websocket Spring server from JUnit test
body: <p>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.</p>
  
  <p>I have a test like </p>
  
  <pre><code> public void measureSpeedWithWebsocket() throws Exception {
      final Waiter subscribeWaiter = new Waiter();
  
      new Thread(() -&gt; {
          // Prepare connection
          WebsocketClient listener = new WebsocketClient("/mytopic/stomp");
          try {
              listener.connectAndSubscribe();
              subscribeWaiter.resume();
          } catch (Exception e) {
              e.printStackTrace();
          }
      }).start();
      subscribeWaiter.await(); // Wait for connection.
  </code></pre>
  
  <p>Here I made use of Waiter from <a href="https://github.com/jhalterman/concurrentunit" rel="noreferrer">https://github.com/jhalterman/concurrentunit</a>, which effect is basically to delay main thread of the test till secondary thread call resume(). <strong>This is likely wrong, because Spring server that is running in the context has to react</strong></p>
  
  <p>I am getting the following error</p>
  
  <pre><code>[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
  </code></pre>
  
  <p>How I can possibly make a proper test that "self-connects" to the websocket offered by my Spring Boot application?</p>