Update MockMvc section on Streaming in the docs

Closes gh-32687
This commit is contained in:
rstoyanchev 2024-05-13 10:49:02 +01:00
parent 89ce63f1f3
commit 1b60b86bb2
2 changed files with 11 additions and 33 deletions

View File

@ -1,7 +1,7 @@
[[spring-mvc-test-vs-end-to-end-integration-tests]] [[spring-mvc-test-vs-end-to-end-integration-tests]]
= MockMvc vs End-to-End Tests = MockMvc vs End-to-End Tests
MockMVc is built on Servlet API mock implementations from the MockMvc is built on Servlet API mock implementations from the
`spring-test` module and does not rely on a running container. Therefore, there are `spring-test` module and does not rely on a running container. Therefore, there are
some differences when compared to full end-to-end integration tests with an actual some differences when compared to full end-to-end integration tests with an actual
client and a live server running. client and a live server running.

View File

@ -1,38 +1,16 @@
[[spring-mvc-test-vs-streaming-response]] [[spring-mvc-test-vs-streaming-response]]
= Streaming Responses = Streaming Responses
The best way to test streaming responses such as Server-Sent Events is through the You can use `WebTestClient` to test xref:testing/webtestclient.adoc#webtestclient-stream[streaming responses]
<<WebTestClient>> which can be used as a test client to connect to a `MockMvc` instance such as Server-Sent Events. However, `MockMvcWebTestClient` doesn't support infinite
to perform tests on Spring MVC controllers without a running server. For example: streams because there is no way to cancel the server stream from the client side.
To test infinite streams, you'll need to
xref:testing/webtestclient.adoc#webtestclient-server-config[bind to] a running server,
or when using Spring Boot,
{spring-boot-docs}/spring-boot-features.html#boot-features-testing-spring-boot-applications-testing-with-running-server[test with a running server].
[tabs] `MockMvcWebTestClient` does support asynchronous responses, and even streaming responses.
====== The limitation is that it can't influence the server to stop, and therefore the server
Java:: must finish writing the response on its own.
+
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
----
WebTestClient client = MockMvcWebTestClient.bindToController(new SseController()).build();
FluxExchangeResult<Person> 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
{spring-boot-docs}/spring-boot-features.html#boot-features-testing-spring-boot-applications-testing-with-running-server[test a running server].