Skip to content

Commit 19859fd

Browse files
committed
Add section on test STOMP/WebSocket applications
Issue: SPR-11266
1 parent c376ee9 commit 19859fd

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

src/asciidoc/index.adoc

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36530,7 +36530,12 @@ server-side while <<websocket-fallback>> explains the SockJS protocol and shows
3653036530
how to configure and use it.
3653136531

3653236532
<<websocket-stomp-overview>> introduces the STOMP messaging protocol.
36533-
36533+
<<websocket-stomp-enable>> demonstrates how to configure STOMP support in Spring.
36534+
<<websocket-stomp-handle>> explains how to use it including writing annotated message
36535+
handling methods, sending messages, choosing message broker options, as
36536+
well as working with the special "user" destinations. Finally
36537+
<<websocket-stomp-testing>> lists three approaches to testing STOMP/WebSocket
36538+
applications.
3653436539

3653536540

3653636541

@@ -37612,6 +37617,47 @@ So in that case the client could subscribe to `/user/exchange/amq.direct/positio
3761237617
ActiveMQ has http://activemq.apache.org/delete-inactive-destinations.html[configuration options]
3761337618
for purging inactive destinations.
3761437619

37620+
[[websocket-stomp-testing]]
37621+
===== Testing Message Handling Controllers
37622+
37623+
There are two main approaches to testing applications using Spring's STOMP over
37624+
WebSocket support. The first is to write server-side tests verifying the functionality
37625+
of controllers and their annotated message handling methods. The second is to write
37626+
full end-to-end tests that involve running a client and a server.
37627+
37628+
The two approaches are not mutually exclusive. On the contrary each has a place
37629+
in an overall test strategy. Server-side tests are more focused and easier to write
37630+
and maintain. End-to-end integration tests on the other hand are more complete and
37631+
test much more but they're also more involved to write and maintain.
37632+
37633+
The simplest form of server-side tests is to write controller unit tests. However
37634+
this is not useful enough since much of what a controller does depends on its
37635+
annotations. Pure unit tests simply can't test that.
37636+
37637+
Ideally controllers under test should be invoked as they are at runtime, much like
37638+
the approach to testing controllers handling HTTP requests using the Spring MVC Test
37639+
framework. i.e. without running a Servlet container but relying on the Spring Framework
37640+
to invoke the annotated controllers. Just like with Spring MVC Test here there are two
37641+
two possible alternatives, either using a "context-based" or "standalone" setup:
37642+
37643+
1. Load the actual Spring configuration with the help of the
37644+
Spring TestContext framework, inject "clientInboundChannel" as a test field, and
37645+
use it to send messages to be handled by controller methods.
37646+
37647+
2. Manually set up the minimum Spring framework infrastructure required to invoke
37648+
controllers (namely the `SimpAnnotationMethodMessageHandler`) and pass messages for
37649+
controllers directly to it.
37650+
37651+
Both of these setup scenarios are demonstrated in the
37652+
https://github.com/rstoyanchev/spring-websocket-portfolio/tree/master/src/test/java/org/springframework/samples/portfolio/web[tests for the stock portfolio]
37653+
sample application.
37654+
37655+
The second approach is to create end-to-end integration tests. For that you will need
37656+
to run a WebSocket server in embedded mode and connect to it as a WebSocket client
37657+
sending WebSocket messages containing STOMP frames.
37658+
The https://github.com/rstoyanchev/spring-websocket-portfolio/tree/master/src/test/java/org/springframework/samples/portfolio/web[tests for the stock portfolio]
37659+
sample application also demonstrate this approach using Tomcat as the embedded
37660+
WebSocket server and a simple STOMP client for test purposes.
3761537661

3761637662
[[spring-integration]]
3761737663
= Integration

0 commit comments

Comments
 (0)