Testing

  • 服务器端测试:专注于验证控制器和已注释的消息处理方法,使用 Spring TestContext 框架或手动设置 Spring 框架来调用控制器。

  • 端到端集成测试:更全面,运行 WebSocket 服务器和客户端并发送 STOMP 消息,以测试应用程序的完整行为。

在你使用Spring的STOMP-over-WebSocket支持时,有两种主要的测试应用程序的方法。首先是编写服务器端测试来验证控制器及其带注释的消息处理方法的功能。第二是编写涉及运行客户端和服务器的完整的端到端测试。

There are two main approaches to testing applications when you use Spring’s STOMP-over-WebSocket support. The first is to write server-side tests to verify the functionality of controllers and their annotated message-handling methods. The second is to write full end-to-end tests that involve running a client and a server.

这两种方法并不相互排斥。恰好相反,每种方法在整个测试策略中都占有一席之地。服务端测试更集中,更容易编写和维护。另一方面,端到端集成测试更完整,测试内容更多,但编写和维护起来也更复杂。

The two approaches are not mutually exclusive. On the contrary, each has a place in an overall test strategy. Server-side tests are more focused and easier to write and maintain. End-to-end integration tests, on the other hand, are more complete and test much more, but they are also more involved to write and maintain.

服务端测试最简单的形式是编写控制器单元测试。然而,这种方法还不够有用,因为控制器的大部分功能依赖于它的注释。纯单元测试根本无法测试这一点。

The simplest form of server-side tests is to write controller unit tests. However, this is not useful enough, since much of what a controller does depends on its annotations. Pure unit tests simply cannot test that.

理想情况下,要测试的控制器应该在运行时被调用,这与通过使用 Spring MVC Test 框架来测试处理 HTTP 请求的控制器的做法非常相似——也就是说,不运行 Servlet 容器,而依靠 Spring 框架来调用注释的控制器。与 Spring MVC Test 一样,这里有两种可能的替代方案,一种是使用 “基于上下文的” 设置,另一种是使用 “独立的” 设置:

Ideally, controllers under test should be invoked as they are at runtime, much like the approach to testing controllers that handle HTTP requests by using the Spring MVC Test framework — that is, without running a Servlet container but relying on the Spring Framework to invoke the annotated controllers. As with Spring MVC Test, you have two possible alternatives here, either use a “context-based” or use a “standalone” setup:

  • Load the actual Spring configuration with the help of the Spring TestContext framework, inject clientInboundChannel as a test field, and use it to send messages to be handled by controller methods.

  • Manually set up the minimum Spring framework infrastructure required to invoke controllers (namely the SimpAnnotationMethodMessageHandler) and pass messages for controllers directly to it.

上述两种安装方案都展示在 tests for the stock portfolio样例应用程序中。

Both of these setup scenarios are demonstrated in the tests for the stock portfolio sample application.

第二种方法是创建端到端集成测试。为此,你需要以嵌入式模式运行 WebSocket 服务器,并以发送包含 STOMP 帧的 WebSocket 消息的 WebSocket 客户端形式连接到它。 tests for the stock portfolio示例应用程序还使用 Tomcat 作为嵌入式 WebSocket 服务器和一个简单的 STOMP 客户端进行测试来演示此方法。

The second approach is to create end-to-end integration tests. For that, you need to run a WebSocket server in embedded mode and connect to it as a WebSocket client that sends WebSocket messages containing STOMP frames. The tests for the stock portfolio sample application also demonstrate this approach by using Tomcat as the embedded WebSocket server and a simple STOMP client for test purposes.