-
Notifications
You must be signed in to change notification settings - Fork 37.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update MockMvc section on Streaming in the docs
Closes gh-32687
- Loading branch information
1 parent
89ce63f
commit 1b60b86
Showing
2 changed files
with
12 additions
and
34 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...OT/pages/testing/spring-mvc-test-framework/vs-end-to-end-integration-tests.adoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 11 additions & 33 deletions
44
...modules/ROOT/pages/testing/spring-mvc-test-framework/vs-streaming-response.adoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,16 @@ | ||
[[spring-mvc-test-vs-streaming-response]] | ||
= Streaming Responses | ||
|
||
The best way to test streaming responses such as Server-Sent Events is through the | ||
<<WebTestClient>> 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: | ||
|
||
[tabs] | ||
====== | ||
Java:: | ||
+ | ||
[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]. | ||
You can use `WebTestClient` to test xref:testing/webtestclient.adoc#webtestclient-stream[streaming responses] | ||
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 | ||
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]. | ||
|
||
`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 | ||
must finish writing the response on its own. | ||
|
||
|