2

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

1 Answer 1

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);
}
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.