我目前正在试验 Spring 4.0 中添加的对 WebSockets 的支持,如中所述本指南 http://spring.io/guides/gs/messaging-stomp-websocket/。如指南中所示,用注释的方法@MessageMapping
可以添加到任何 Spring MVC 控制器,其中还可能包含@RequestMapping
方法。
spring-test 模块支持编写集成测试@RequestMapping
方法(如描述的here http://docs.spring.io/spring/docs/4.0.0.RELEASE/spring-framework-reference/html/testing.html#spring-mvc-test-framework)以一种非常简单和流畅的方式:
@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"));
}
是否有类似的测试支持@MessageMapping
使用 WebSocket 的方法?我没有在任何 Spring 模块中找到任何内容,并且所有 WebSocket 指南都不包含任何测试。如果没有,我是否需要实际部署应用程序并使用WebSocketConnectionManager http://docs.spring.io/autorepo/docs/spring/4.0.0.RELEASE/javadoc-api/org/springframework/web/socket/client/WebSocketConnectionManager.html连接测试客户端?或者我可以通过 spring-test 构建一些 API 吗?
这个示例项目 https://github.com/rstoyanchev/spring-websocket-test/blob/master/src/test/java/org/springframework/samples/websocket/echo/StandardClientApp.java包含如此小的测试客户端,但我更愿意将其集成到实际测试中,而不需要我部署应用程序并在测试中硬编码部署路径。
目前还没有像 Spring MVC Test 这样的 @MessageMapping 方法。然而,即使没有流畅的 API,类似的测试方法也应该是可能的。 JIRA 中有一张票(参见https://jira.spring.io/browse/SPR-11266 https://jira.spring.io/browse/SPR-11266)提供文档,因此请在不久的将来查看该票以获取更多详细信息。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)