Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing @MessageMapping WebSocket methods of Spring MVC controllers

I am currently experimenting with the support for WebSockets added in Spring 4.0, as described in this guide. As demonstrated in the guide, methods annotated with @MessageMapping can be added to any Spring MVC controller, which may also contain @RequestMapping methods.

The spring-test module has support for writing integration tests for @RequestMapping methods (as described here) in a very simple and fluid way:

@Test
public void getAccount() throws Exception {
    this.mockMvc.perform(get("/accounts/1").accept(MediaType.parseMediaType("application/json;charset=UTF-8")))
        .andExpect(status().isOk())
        .andExpect(content().contentType("application/json"))
        .andExpect(jsonPath("$.name").value("Lee"));
}

Is there similar support for testing @MessageMapping methods using WebSockets? I have not found anything in any of the Spring modules, and none of the WebSocket guides contain any tests. If not, would I need to actually deploy the application and use a WebSocketConnectionManager to connect a test client? Or is there some API I can build on from spring-test?

This sample project contains such a small test client, but I would prefer to integrate this into the actual tests without requiring me to deploy the application and hardcode the deployed path in the tests.

like image 803
andersschuller Avatar asked Sep 20 '25 07:09

andersschuller


1 Answers

There is nothing like Spring MVC Test for @MessageMapping methods yet. However, a similar approach to testing should be possible even without the fluent API. There is a ticket in JIRA (see https://jira.spring.io/browse/SPR-11266) to provide documentation so watch that ticket for more details in the very near future.

like image 111
Rossen Stoyanchev Avatar answered Sep 22 '25 03:09

Rossen Stoyanchev