【问题标题】:Testing @MessageMapping WebSocket methods of Spring MVC controllers测试 Spring MVC 控制器的 @MessageMapping WebSocket 方法
【发布时间】:2014-01-07 12:55:53
【问题描述】:

我目前正在试验对 Spring 4.0 中添加的 WebSockets 的支持,如this guide 中所述。如指南中所示,使用 @MessageMapping 注释的方法可以添加到任何 Spring MVC 控制器中,其中也可能包含 @RequestMapping 方法。

spring-test 模块支持以非常简单流畅的方式为@RequestMapping 方法(如here 所述)编写集成测试:

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

是否有类似的支持使用 WebSockets 测试 @MessageMapping 方法?我在任何 Spring 模块中都没有发现任何东西,而且 WebSocket 指南中也没有包含任何测试。如果没有,我是否需要实际部署应用程序并使用WebSocketConnectionManager 连接测试客户端?还是有一些我可以从 spring-test 构建的 API?

This sample project 包含这么小的测试客户端,但我更愿意将它集成到实际测试中,而不需要我在测试中部署应用程序和硬编码部署路径。

【问题讨论】:

    标签: java spring spring-mvc spring-test


    【解决方案1】:

    对于@MessageMapping 方法,还没有像Spring MVC Test 这样的东西。但是,即使没有 fluent API,也应该可以使用类似的测试方法。 JIRA 中有一张票(请参阅https://jira.spring.io/browse/SPR-11266)以提供文档,因此请在不久的将来查看该票以了解更多详细信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-11
      • 2021-04-01
      • 2013-04-18
      相关资源
      最近更新 更多