Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何对SpringWebSocketStomClient进行单元测试_Websocket_Spring Websocket - Fatal编程技术网

如何对SpringWebSocketStomClient进行单元测试

如何对SpringWebSocketStomClient进行单元测试,websocket,spring-websocket,Websocket,Spring Websocket,我有一个Stompclient,它连接到Spring引导服务器并执行一些订阅。此websocket客户端的代码覆盖率为0%。我只能找到如何对Spring boot Websocket服务器进行单元测试的代码示例。但这是客户端验证Stompclient是否正常工作。如果我的问题缺少任何细节,请告诉我 这是我的示例连接方法,我需要为其编写单元测试用例 StompSession connect(String connectionUrl) throws Exception { WebSocket

我有一个Stompclient,它连接到Spring引导服务器并执行一些订阅。此websocket客户端的代码覆盖率为0%。我只能找到如何对Spring boot Websocket服务器进行单元测试的代码示例。但这是客户端验证Stompclient是否正常工作。如果我的问题缺少任何细节,请告诉我

这是我的示例连接方法,我需要为其编写单元测试用例

StompSession connect(String connectionUrl) throws Exception {
    WebSocketClient transport = new StandardWebSocketClient();
    WebSocketStompClient stompClient = new WebSocketStompClient(transport);
    stompClient.setMessageConverter(new StringMessageConverter());
    ListenableFuture<StompSession> stompSession = stompClient.connect(connectionUrl, new WebSocketHttpHeaders(), new MyHandler());
    return stompSession.get();
}
StompSession connect(字符串连接URL)引发异常{
WebSocketClient传输=新标准WebSocketClient();
WebSocketStompClient stompClient=新的WebSocketStompClient(传输);
setMessageConverter(新的StringMessageConverter());
ListenableFuture stompSession=stompClient.connect(connectionUrl,new-WebSocketHttpHeaders(),new-MyHandler());
返回stompSession.get();
}

注意:客户端是轻量级SDK的一部分,因此它不能对这个单元测试有很大的依赖性

感谢Artem的建议,我查看了Spring websocket测试示例。下面是我如何为自己解决的,希望这能帮助别人

public class WebSocketStompClientTests {

private static final Logger LOG = LoggerFactory.getLogger(WebSocketStompClientTests.class);

@Rule
public final TestName testName = new TestName();

@Rule
public ErrorCollector collector = new ErrorCollector();

private WebSocketTestServer server;

private AnnotationConfigWebApplicationContext wac;

@Before
public void setUp() throws Exception {

    LOG.debug("Setting up before '" + this.testName.getMethodName() + "'");

    this.wac = new AnnotationConfigWebApplicationContext();
    this.wac.register(TestConfig.class);
    this.wac.refresh();

    this.server = new TomcatWebSocketTestServer();
    this.server.setup();
    this.server.deployConfig(this.wac);
    this.server.start();
}

@After
public void tearDown() throws Exception {
    try {
        this.server.undeployConfig();
    }
    catch (Throwable t) {
        LOG.error("Failed to undeploy application config", t);
    }
    try {
        this.server.stop();
    }
    catch (Throwable t) {
        LOG.error("Failed to stop server", t);
    }
    try {
        this.wac.close();
    }
    catch (Throwable t) {
        LOG.error("Failed to close WebApplicationContext", t);
    }
}

@Configuration
static class TestConfig extends WebSocketMessageBrokerConfigurationSupport {

    @Override
    protected void registerStompEndpoints(StompEndpointRegistry registry) {
        // Can't rely on classpath detection
        RequestUpgradeStrategy upgradeStrategy = new TomcatRequestUpgradeStrategy();
        registry.addEndpoint("/app")
                .setHandshakeHandler(new DefaultHandshakeHandler(upgradeStrategy))
                .setAllowedOrigins("*")
                .withSockJS();
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry configurer) {
        configurer.setApplicationDestinationPrefixes("/publish");
        configurer.enableSimpleBroker("/topic", "/queue");
    }
}

@Test
public void testConnect() {
   TestStompClient stompClient = TestStompClient.connect();
   assert(true);
}
}

作为示例,这对您有用吗:?