mirror of
https://github.com/spring-projects/spring-framework
synced 2026-06-08 17:33:33 +00:00
Add MockRestServiceServer#createServer variant for RestClient
Prior to this commit, `MockRestServiceServer` would provide `createServer` shortcuts for `RestTemplate` and `RestGatewaySupport`. Now that `RestClient` offers a modern client variant, we should provide the same for `RestClient`. Closes gh-36572
This commit is contained in:
+16
-5
@@ -33,9 +33,9 @@ import org.springframework.web.client.support.RestGatewaySupport;
|
||||
|
||||
/**
|
||||
* <strong>Main entry point for client-side REST testing</strong>. Used for tests
|
||||
* that involve direct or indirect use of the {@link RestTemplate}. Provides a
|
||||
* that involve direct or indirect use of the {@link RestClient}. Provides a
|
||||
* way to set up expected requests that will be performed through the
|
||||
* {@code RestTemplate} as well as mock responses to send back thus removing the
|
||||
* {@code RestClient} as well as mock responses to send back thus removing the
|
||||
* need for an actual server.
|
||||
*
|
||||
* <p>Below is an example that assumes static imports from
|
||||
@@ -43,13 +43,14 @@ import org.springframework.web.client.support.RestGatewaySupport;
|
||||
* and {@code ExpectedCount}:
|
||||
*
|
||||
* <pre class="code">
|
||||
* RestTemplate restTemplate = new RestTemplate()
|
||||
* MockRestServiceServer server = MockRestServiceServer.bindTo(restTemplate).build();
|
||||
* RestClient.Builder clientBuilder = RestClient.builder();
|
||||
* MockRestServiceServer server = MockRestServiceServer.bindTo(clientBuilder).build();
|
||||
* RestClient restClient = clientBuilder.build();
|
||||
*
|
||||
* server.expect(manyTimes(), requestTo("/hotels/42")).andExpect(method(HttpMethod.GET))
|
||||
* .andRespond(withSuccess("{ \"id\" : \"42\", \"name\" : \"Holiday Inn\"}", MediaType.APPLICATION_JSON));
|
||||
*
|
||||
* Hotel hotel = restTemplate.getForObject("/hotels/{id}", Hotel.class, 42);
|
||||
* Hotel hotel = restClient.get().uri("/hotels/{id}", 42).retrieve().body(Hotel.class);
|
||||
* // Use the hotel instance...
|
||||
*
|
||||
* // Verify all expectations met
|
||||
@@ -167,6 +168,16 @@ public final class MockRestServiceServer {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* A shortcut for {@code bindTo(clientBuilder).build()}.
|
||||
* @param clientBuilder the RestClient builder to set up for mock testing
|
||||
* @return the mock server
|
||||
* @since 7.0.7
|
||||
*/
|
||||
public static MockRestServiceServer createServer(RestClient.Builder clientBuilder) {
|
||||
return bindTo(clientBuilder).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* A shortcut for {@code bindTo(restTemplate).build()}.
|
||||
* @param restTemplate the RestTemplate to set up for mock testing
|
||||
|
||||
Reference in New Issue
Block a user