There is no @WebSocketTest annotation in Spring Boot. Spring Boot ships test slices such as @WebMvcTest, @WebFluxTest and @DataJpaTest, but none for WebSockets, and examples that use @WebSocketTest or a WebSocketTestClient do not compile against any released Spring version. To test WebSocket endpoints you start the application with @SpringBootTest on a random port and connect with a real client.
The right client depends on your stack: WebSocketStompClient for STOMP endpoints, StandardWebSocketClient for raw handlers on Spring MVC, and ReactorNettyWebSocketClient for Spring WebFlux.
Test slices in Spring Boot load a narrow part of the context and mock the rest. A WebSocket endpoint needs a running server with the HTTP upgrade handshake, so MockMvc cannot exercise it: MockMvc simulates servlet requests without a network connection and has no upgrade support. That is why the Boot team offers no WebSocket slice and why the usual recommendation is a full application test with webEnvironment = RANDOM_PORT.
If you find @WebSocketTest in a tutorial, treat the whole tutorial with caution. It describes an API that does not exist.
Assume a server configured with @EnableWebSocketMessageBroker, an endpoint at /ws, and a controller mapped to /app/chat that broadcasts to /topic/chat.
Key points:
@LocalServerPort injects the port Boot picked, so tests never collide.LinkedBlockingQueue with poll(timeout) bridges the asynchronous callback to the test thread without Thread.sleep.For a WebSocketHandler registered with @EnableWebSocket, use StandardWebSocketClient and a TextWebSocketHandler that records replies.
In a WebFlux application, use ReactorNettyWebSocketClient. It returns a Mono<Void> that completes when your handler's session finishes.
Always put a timeout on block so a broken handler fails the test instead of hanging the build.
| Server code | Test client | Test setup |
|---|---|---|
STOMP with @EnableWebSocketMessageBroker | WebSocketStompClient | @SpringBootTest with RANDOM_PORT |
WebSocketHandler on Spring MVC | StandardWebSocketClient | @SpringBootTest with RANDOM_PORT |
WebSocketHandler on WebFlux | ReactorNettyWebSocketClient | @SpringBootTest with RANDOM_PORT |
| Handler logic only (no transport) | Plain unit test with a mocked WebSocketSession | No Spring context |
Extracting message-handling logic into a plain service lets you unit test most behavior without any network, and keep one or two socket-level tests for the handshake, security and serialization.
No. Spring has WebTestClient for HTTP on WebFlux and MockMvc for servlet requests, but neither opens WebSocket connections. Use the real clients shown above.
Not for the WebSocket itself. MockMvc has no connection upgrade, so it can only test the plain HTTP parts of your application, such as a REST endpoint next to the socket.
Subscribe before you send, use blocking queues or Awaitility with timeouts instead of fixed sleeps, and use a random port so parallel builds do not conflict.
Pass credentials with WebSocketHttpHeaders for the handshake or StompHeaders for the CONNECT frame, and assert that a missing token fails the connection.
Spring Boot has no @WebSocketTest annotation. Test WebSocket code by starting the real server with @SpringBootTest(webEnvironment = RANDOM_PORT) and connecting with WebSocketStompClient, StandardWebSocketClient or ReactorNettyWebSocketClient, depending on your stack. Keep business logic in plain classes so most of it is covered by fast unit tests.