title: Spring-Boot RestClientTest not correctly auto-configuring MockRestServiceServer due to unbound RestTemplate
body: <p>EDIT: This question is specifically pertaining to the @RestClientTest annotation introduced in spring-boot 1.4.0 which is intended to replace the factory method.</p>
  
  <h2>Problem:</h2>
  
  <p>According to the <a href="http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-testing-spring-boot-applications-testing-autoconfigured-rest-client" rel="noreferrer">documentation</a> the @RestClientTest should correctly configure a MockRestServiceServer to use when testing a REST client.  However when running a test I am getting an IllegalStateException saying the MockServerRestTemplateCustomizer has not been bound to a RestTemplate.</p>
  
  <p>Its worth noting that I'm using Gson for deserialization and not Jackson, hence the exclude.</p>
  
  <p>Does anyone know how to correctly use this new annotation?  I haven't found any examples that require more configuration then when I have already.</p>
  
  <p>Configuration:</p>
  
  <pre><code>@SpringBootConfiguration
  @ComponentScan
  @EnableAutoConfiguration(exclude = {JacksonAutoConfiguration.class})
  public class ClientConfiguration {
  
  ...
  
     @Bean
     public RestTemplateBuilder restTemplateBuilder() {
         return new RestTemplateBuilder()
              .rootUri(rootUri)
              .basicAuthorization(username, password);
     }
  }
  </code></pre>
  
  <p>Client:</p>
  
  <pre><code>@Service
  public class ComponentsClientImpl implements ComponentsClient {
  
      private RestTemplate restTemplate;
  
      @Autowired
      public ComponentsClientImpl(RestTemplateBuilder builder) {
          this.restTemplate = builder.build();
      }
  
      public ResponseDTO getComponentDetails(RequestDTO requestDTO) {
          HttpEntity&lt;RequestDTO&gt; entity = new HttpEntity&lt;&gt;(requestDTO);
          ResponseEntity&lt;ResponseDTO&gt; response = 
                restTemplate.postForEntity("/api", entity, ResponseDTO.class);
          return response.getBody();
      }
  }
  </code></pre>
  
  <p>Test</p>
  
  <pre><code>@RunWith(SpringRunner.class)
  @RestClientTest(ComponentsClientImpl.class)
  public class ComponentsClientTest {
  
      @Autowired
      private ComponentsClient client;
  
      @Autowired
      private MockRestServiceServer server;
  
      @Test
      public void getComponentDetailsWhenResultIsSuccessShouldReturnComponentDetails() throws Exception {
          server.expect(requestTo("/api"))
              .andRespond(withSuccess(getResponseJson(), APPLICATION_JSON));
  
          ResponseDTO response = client.getComponentDetails(requestDto);
          ResponseDTO expected = responseFromJson(getResponseJson());
  
          assertThat(response, is(expectedResponse));
      }
  }
  </code></pre>
  
  <p>And the Exception:</p>
  
  <pre><code>java.lang.IllegalStateException: Unable to use auto-configured MockRestServiceServer since MockServerRestTemplateCustomizer has not been bound to a RestTemplate
  </code></pre>
  
  <h2>Answer:</h2>
  
  <p>As per the answer below there is no need to declare a RestTemplateBuilder bean into the context as it is already provided by the spring-boot auto-configuration.</p>
  
  <p>If the project is a spring-boot application (it has @SpringBootApplication annotation) this will work as intended.  In the above case however the project was a client-library and thus had no main application.  </p>
  
  <p>In order to ensure the RestTemplateBuilder was injected correctly in the main application context (the bean having been removed) the component scan needs a CUSTOM filter (the one used by @SpringBootApplication)</p>
  
  <pre><code>@ComponentScan(excludeFilters = {
          @ComponentScan.Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class)
  })
  </code></pre>