Class MockServerRestTemplateCustomizer

java.lang.Object
org.springframework.boot.test.web.client.MockServerRestTemplateCustomizer
All Implemented Interfaces:
org.springframework.boot.web.client.RestTemplateCustomizer

public class MockServerRestTemplateCustomizer extends Object implements org.springframework.boot.web.client.RestTemplateCustomizer
RestTemplateCustomizer that can be applied to a RestTemplateBuilder instances to add MockRestServiceServer support.

Typically applied to an existing builder before it is used, for example:

 MockServerRestTemplateCustomizer customizer = new MockServerRestTemplateCustomizer();
 MyBean bean = new MyBean(new RestTemplateBuilder(customizer));
 customizer.getServer().expect(requestTo("/hello")).andRespond(withSuccess());
 bean.makeRestCall();
 

If the customizer is only used once, the getServer() method can be used to obtain the mock server. If the customizer has been used more than once the getServer(RestTemplate) or getServers() method must be used to access the related server.

Since:
1.4.0
See Also:
  • Constructor Details

    • MockServerRestTemplateCustomizer

      public MockServerRestTemplateCustomizer()
    • MockServerRestTemplateCustomizer

      public MockServerRestTemplateCustomizer(Class<? extends org.springframework.test.web.client.RequestExpectationManager> expectationManager)
      Crate a new MockServerRestTemplateCustomizer instance.
      Parameters:
      expectationManager - the expectation manager class to use
    • MockServerRestTemplateCustomizer

      public MockServerRestTemplateCustomizer(Supplier<? extends org.springframework.test.web.client.RequestExpectationManager> expectationManagerSupplier)
      Crate a new MockServerRestTemplateCustomizer instance.
      Parameters:
      expectationManagerSupplier - a supplier that provides the RequestExpectationManager to use
      Since:
      3.0.0
  • Method Details

    • setDetectRootUri

      public void setDetectRootUri(boolean detectRootUri)
      Set if root URIs from RootUriRequestExpectationManager should be detected and applied to the MockRestServiceServer.
      Parameters:
      detectRootUri - if root URIs should be detected
    • customize

      public void customize(org.springframework.web.client.RestTemplate restTemplate)
      Specified by:
      customize in interface org.springframework.boot.web.client.RestTemplateCustomizer
    • createExpectationManager

      protected org.springframework.test.web.client.RequestExpectationManager createExpectationManager()
    • getServer

      public org.springframework.test.web.client.MockRestServiceServer getServer()
    • getExpectationManagers

      public Map<org.springframework.web.client.RestTemplate,org.springframework.test.web.client.RequestExpectationManager> getExpectationManagers()
    • getServer

      public org.springframework.test.web.client.MockRestServiceServer getServer(org.springframework.web.client.RestTemplate restTemplate)
    • getServers

      public Map<org.springframework.web.client.RestTemplate,org.springframework.test.web.client.MockRestServiceServer> getServers()