从 JUnit 测试自连接到 Websocket Spring 服务器

Self-Connect to Websocket Spring server from JUnit test

我有一个公开 Websocket/SockJS/Stomp 服务器端点的应用程序,我想 运行 一个 JUnit 测试 运行s 客户端(Java STOMP 客户端,也来自 Spring) 反对它,测试 "sending" 特征。

我有一个像

这样的测试
 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.

这里我使用了https://github.com/jhalterman/concurrentunit的Waiter,其作用基本上是延迟测试的主线程,直到辅助线程调用resume()。 这很可能是错误的,因为 Spring 在上下文中 运行ning 的服务器必须做出反应

我收到以下错误

[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

我如何才能对 "self-connects" 我的 Spring 启动应用程序提供的 websocket 进行适当的测试?

如果您使用的是 Spring Boot,那您就走运了:)。 这是一个 http://rafaelhz.github.io/testing-websockets/ 如何测试网络套接字的示例,它在 Spring 引导中完美运行,可以为您提供很多帮助。我试图在 Spring MVC 中做同样的事情,但不幸的是,这在 Spring MVC 中不起作用。