From ca9eb57c9485bce02fd68710cd83aaad5aa2cf66 Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Mon, 13 May 2024 12:40:46 +0100 Subject: [PATCH] Update MockMvc section on Streaming in the docs Closes gh-32687 --- src/docs/asciidoc/testing.adoc | 36 +++++++--------------------------- 1 file changed, 7 insertions(+), 29 deletions(-) diff --git a/src/docs/asciidoc/testing.adoc b/src/docs/asciidoc/testing.adoc index d0a5c945d5ed..264a2227da09 100644 --- a/src/docs/asciidoc/testing.adoc +++ b/src/docs/asciidoc/testing.adoc @@ -7670,35 +7670,13 @@ or reactive type such as Reactor `Mono`: [[spring-mvc-test-vs-streaming-response]] ===== Streaming Responses -The best way to test streaming responses such as Server-Sent Events is through the -<> which can be used as a test client to connect to a `MockMvc` instance -to perform tests on Spring MVC controllers without a running server. For example: - -[source,java,indent=0,subs="verbatim,quotes",role="primary"] -.Java ----- - WebTestClient client = MockMvcWebTestClient.bindToController(new SseController()).build(); - - FluxExchangeResult exchangeResult = client.get() - .uri("/persons") - .exchange() - .expectStatus().isOk() - .expectHeader().contentType("text/event-stream") - .returnResult(Person.class); - - // Use StepVerifier from Project Reactor to test the streaming response - - StepVerifier.create(exchangeResult.getResponseBody()) - .expectNext(new Person("N0"), new Person("N1"), new Person("N2")) - .expectNextCount(4) - .consumeNextWith(person -> assertThat(person.getName()).endsWith("7")) - .thenCancel() - .verify(); ----- - -`WebTestClient` can also connect to a live server and perform full end-to-end integration -tests. This is also supported in Spring Boot where you can -{doc-spring-boot}/html/spring-boot-features.html#boot-features-testing-spring-boot-applications-testing-with-running-server[test a running server]. +You can use `WebTestClient` to test <> +such as Server-Sent Events. However, `MockMvcWebTestClient` doesn't support infinite +streams because there is no way to cancel the server stream from the client side. +To test infinite streams, you'll need to +<> a running server, +or when using Spring Boot, +{doc-spring-boot}/html/spring-boot-features.html#boot-features-testing-spring-boot-applications-testing-with-running-server[test with a running server]. [[spring-mvc-test-server-filters]]