open class MockRestServiceServer
Main entry point for client-side REST testing. Used for tests that involve direct or indirect use of the RestTemplate. Provides a way to set up expected requests that will be performed through the RestTemplate
as well as mock responses to send back thus removing the need for an actual server.
Below is an example that assumes static imports from MockRestRequestMatchers
, MockRestResponseCreators
, and ExpectedCount
:
RestTemplate restTemplate = new RestTemplate() MockRestServiceServer server = MockRestServiceServer.bindTo(restTemplate).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); // Use the hotel instance... // Verify all expectations met server.verify();
Note that as an alternative to the above you can also set the MockMvcClientHttpRequestFactory on a RestTemplate
which allows executing requests against an instance of org.springframework.test.web.servlet.MockMvc.
Author
Craig Walls
Author
Rossen Stoyanchev
Since
3.2
open static fun bindTo(restTemplate: RestTemplate): MockRestServiceServerBuilder
Return a builder for a open static fun bindTo(asyncRestTemplate: AsyncRestTemplate): MockRestServiceServerBuilder
Return a builder for a open static fun bindTo(restGateway: RestGatewaySupport): MockRestServiceServerBuilder
Return a builder for a |
|
open static fun createServer(restTemplate: RestTemplate): MockRestServiceServer
A shortcut for open static fun createServer(asyncRestTemplate: AsyncRestTemplate): MockRestServiceServer
A shortcut for open static fun createServer(restGateway: RestGatewaySupport): MockRestServiceServer
A shortcut for |
|
open fun expect(matcher: RequestMatcher): ResponseActions
Set up an expectation for a single HTTP request. The returned ResponseActions can be used to set up further expectations as well as to define the response. This method may be invoked any number times before starting to make request through the underlying open fun expect(count: ExpectedCount, matcher: RequestMatcher): ResponseActions
An alternative to When request expectations have an expected count greater than one, only the first execution is expected to match the order of declaration. Subsequent request executions may be inserted anywhere thereafter. |
|
open fun reset(): Unit
Reset the internal state removing all expectations and recorded requests. |
|
open fun verify(): Unit
Verify that all expected requests set up via |