Polishing

See gh-29721
This commit is contained in:
rstoyanchev
2023-01-06 16:50:19 +00:00
parent 4bcc24372a
commit c702cd418e
5 changed files with 59 additions and 102 deletions

View File

@@ -117,21 +117,21 @@ logic but without running a server. The following example shows how to do so:
// Test code that uses the above RestTemplate ...
----
In the more specific cases where total isolation isn't desired and some integration testing
of one or more calls is needed, a specific `ResponseCreator` can be set up in advance and
used to perform actual requests and assert the response.
The following example shows how to set up and use the `ExecutingResponseCreator` to do so:
In some cases it may be necessary to perform an actual call to a remote service instead
of mocking the response. The following example shows how to do that through
`ExecutingResponseCreator`:
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
----
RestTemplate restTemplate = new RestTemplate();
// Make sure to capture the request factory of the RestTemplate before binding
// Create ExecutingResponseCreator with the original request factory
ExecutingResponseCreator withActualResponse = new ExecutingResponseCreator(restTemplate.getRequestFactory());
MockRestServiceServer mockServer = MockRestServiceServer.bindTo(restTemplate).build();
mockServer.expect(requestTo("/greeting")).andRespond(withActualResponse);
mockServer.expect(requestTo("/profile")).andRespond(withSuccess());
mockServer.expect(requestTo("/quoteOfTheDay")).andRespond(withActualResponse);
// Test code that uses the above RestTemplate ...
@@ -142,7 +142,7 @@ The following example shows how to set up and use the `ExecutingResponseCreator`
----
val restTemplate = RestTemplate()
// Make sure to capture the request factory of the RestTemplate before binding
// Create ExecutingResponseCreator with the original request factory
val withActualResponse = new ExecutingResponseCreator(restTemplate.getRequestFactory())
val mockServer = MockRestServiceServer.bindTo(restTemplate).build()
@@ -156,16 +156,15 @@ The following example shows how to set up and use the `ExecutingResponseCreator`
In the preceding example, we create the `ExecutingResponseCreator` using the
`ClientHttpRequestFactory` from the `RestTemplate` _before_ `MockRestServiceServer` replaces
it with the custom one.
Then we define expectations with two kinds of response:
it with a different one that mocks responses.
Then we define expectations with two kinds of responses:
* a stub `200` response for the `/profile` endpoint (no actual request will be executed)
* an "executing response" for the `/quoteOfTheDay` endpoint
* a response obtained through a call to the `/quoteOfTheDay` endpoint
In the second case, the request is executed by the `ClientHttpRequestFactory` that was
In the second case, the request is executed through the `ClientHttpRequestFactory` that was
captured earlier. This generates a response that could e.g. come from an actual remote server,
depending on how the `RestTemplate` was originally configured, and MockMVC can be further
used to assert the content of the response.
depending on how the `RestTemplate` was originally configured.
[[spring-mvc-test-client-static-imports]]
== Static Imports