Skip to content

Commit c769f43

Browse files
committed
Update docs on testing client code
Closes gh-34892
1 parent 4782c69 commit c769f43

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

framework-docs/modules/ROOT/pages/testing/spring-mvc-test-client.adoc

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
11
[[spring-mvc-test-client]]
22
= Testing Client Applications
33

4-
You can use client-side tests to test code that internally uses the `RestTemplate`. The
5-
idea is to declare expected requests and to provide "`stub`" responses so that you can
6-
focus on testing the code in isolation (that is, without running a server). The following
7-
example shows how to do so:
4+
To test code that uses the `RestClient` or `RestTemplate`, you can use a mock web server, such as
5+
https://github.com/square/okhttp#mockwebserver[OkHttp MockWebServer] or
6+
https://wiremock.org/[WireMock]. Mock web servers accept requests over HTTP like a regular
7+
server, and that means you can test with the same HTTP client that is also configured in
8+
the same way as in production, which is important because there are often subtle
9+
differences in the way different clients handle network I/O. Another advantage of mock
10+
web servers is the ability to simulate specific network issues and conditions at the
11+
transport level, in combination with the client used in production.
12+
13+
In addition to dedicated mock web servers, historically the Spring Framework has provided
14+
a built-in option to test `RestClient` or `RestTemplate` through `MockRestServiceServer`.
15+
This relies on configuring the client under test with a custom `ClientHttpRequestFactory`
16+
backed by the mock server that is in turn set up to expect requests and send "`stub`"
17+
responses so that you can focus on testing the code in isolation, without running a server.
18+
19+
TIP: `MockRestServiceServer` predates the existence of mock web servers. At present, we
20+
recommend using mock web servers for more complete testing of the transport layer and
21+
network conditions.
22+
23+
The following example shows an example of using `MockRestServiceServer`:
824

925
[tabs]
1026
======

framework-docs/modules/ROOT/pages/web/webflux-webclient/client-testing.adoc

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@
22
= Testing
33
:page-section-summary-toc: 1
44

5-
To test code that uses the `WebClient`, you can use a mock web server, such as the
6-
https://github.com/square/okhttp#mockwebserver[OkHttp MockWebServer]. To see an example
7-
of its use, check out
5+
To test code that uses the `WebClient`, you can use a mock web server, such as
6+
https://github.com/square/okhttp#mockwebserver[OkHttp MockWebServer] or
7+
https://wiremock.org/[WireMock]. Mock web servers accept requests over HTTP like a regular
8+
server, and that means you can test with the same HTTP client that is also configured in
9+
the same way as in production, which is important because there are often subtle
10+
differences in the way different clients handle network I/O. Another advantage of mock
11+
web servers is the ability to simulate specific network issues and conditions at the
12+
transport level, in combination with the client used in production.
13+
14+
For example use of MockWebServer, see
815
{spring-framework-code}/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientIntegrationTests.java[`WebClientIntegrationTests`]
916
in the Spring Framework test suite or the
1017
https://github.com/square/okhttp/tree/master/samples/static-server[`static-server`]

0 commit comments

Comments
 (0)