How to unit test Spring WebSocketStompClient

巧了我就是萌 提交于 2021-02-10 05:20:40

问题


I've a Stompclient which connects to a Spring boot server and performs some subscriptions. The code coverage for this websocket client is at 0%. I can only find the code samples for how to unit test Spring boot Websocket server. But this is client side verifying the Stompclient is working fine. Please let me know if my question is missing any details.

Here's my sample connect method which I need to write unit test case for.

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();
}

Note: The client is part of a lightweight SDK so it cannot have heavy dependency for this unit test.


回答1:


Thanks to Artem for suggestion I look into the Spring websocket test examples. Here's how I solved it for me, hope this helps someone.

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);
}
}


来源:https://stackoverflow.com/questions/51734311/how-to-unit-test-spring-websocketstompclient

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!