类 MockRestServiceServer


  • public final class MockRestServiceServer
    extends Object
    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 MockMvc.

    从以下版本开始:
    3.2
    作者:
    Craig Walls, Rossen Stoyanchev