Skip to content

Commit ca9eb57

Browse files
committed
Update MockMvc section on Streaming in the docs
Closes gh-32687
1 parent df83746 commit ca9eb57

File tree

1 file changed

+7
-29
lines changed

1 file changed

+7
-29
lines changed

Diff for: src/docs/asciidoc/testing.adoc

+7-29
Original file line numberDiff line numberDiff line change
@@ -7670,35 +7670,13 @@ or reactive type such as Reactor `Mono`:
76707670
[[spring-mvc-test-vs-streaming-response]]
76717671
===== Streaming Responses
76727672

7673-
The best way to test streaming responses such as Server-Sent Events is through the
7674-
<<WebTestClient>> which can be used as a test client to connect to a `MockMvc` instance
7675-
to perform tests on Spring MVC controllers without a running server. For example:
7676-
7677-
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
7678-
.Java
7679-
----
7680-
WebTestClient client = MockMvcWebTestClient.bindToController(new SseController()).build();
7681-
7682-
FluxExchangeResult<Person> exchangeResult = client.get()
7683-
.uri("/persons")
7684-
.exchange()
7685-
.expectStatus().isOk()
7686-
.expectHeader().contentType("text/event-stream")
7687-
.returnResult(Person.class);
7688-
7689-
// Use StepVerifier from Project Reactor to test the streaming response
7690-
7691-
StepVerifier.create(exchangeResult.getResponseBody())
7692-
.expectNext(new Person("N0"), new Person("N1"), new Person("N2"))
7693-
.expectNextCount(4)
7694-
.consumeNextWith(person -> assertThat(person.getName()).endsWith("7"))
7695-
.thenCancel()
7696-
.verify();
7697-
----
7698-
7699-
`WebTestClient` can also connect to a live server and perform full end-to-end integration
7700-
tests. This is also supported in Spring Boot where you can
7701-
{doc-spring-boot}/html/spring-boot-features.html#boot-features-testing-spring-boot-applications-testing-with-running-server[test a running server].
7673+
You can use `WebTestClient` to test <<testing/testing-webtestclient.adoc#webtestclient-stream,streaming responses>>
7674+
such as Server-Sent Events. However, `MockMvcWebTestClient` doesn't support infinite
7675+
streams because there is no way to cancel the server stream from the client side.
7676+
To test infinite streams, you'll need to
7677+
<<testing/testing-webtestclient.adoc#webtestclient-server-config,bind to>> a running server,
7678+
or when using Spring Boot,
7679+
{doc-spring-boot}/html/spring-boot-features.html#boot-features-testing-spring-boot-applications-testing-with-running-server[test with a running server].
77027680

77037681

77047682
[[spring-mvc-test-server-filters]]

0 commit comments

Comments
 (0)