|
1 | 1 | [[spring-mvc-test-client]]
|
2 | 2 | = Testing Client Applications
|
3 | 3 |
|
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`: |
8 | 24 |
|
9 | 25 | [tabs]
|
10 | 26 | ======
|
|
0 commit comments