title: How to mock REST server before Spring context initialization?
body: <p>I have a Spring Bean that sends a request inside its constructor using <code>RestTemplateBuilder</code> in order to send a request:</p>
  <pre class="lang-java prettyprint-override"><code>@Service
  class MyService {
  
    MySettingsFromRemote settings;
  
    MyService(RestTemplateBuilder builder, @Value(&quot;${my-url}&quot;) String url){
      var rt = builder.build();
      setting = rt.getForEntity(url, MySettingsFromRemote.class);
    }
    
    ...
  }
  </code></pre>
  <p>During testing, I would like to mock the response using <code>MockRestServiceServer</code> (or maybe mock the <code>RestTemplateBuilder</code> that is used to send the request) with some predefined data just so the application context loads. The address is written in the <code>application.properties</code> file.</p>
  <p>I tried doing this:</p>
  <pre><code>import org.junit.Before;
  import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.beans.factory.annotation.Value;
  import org.springframework.boot.test.autoconfigure.web.client.AutoConfigureMockRestServiceServer;
  import org.springframework.boot.test.context.SpringBootTest;
  import org.springframework.http.MediaType;
  import org.springframework.test.context.junit4.SpringRunner;
  import org.springframework.test.web.client.MockRestServiceServer;
  import org.springframework.test.web.client.match.MockRestRequestMatchers;
  
  import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
  
  @RunWith(SpringRunner.class)
  @AutoConfigureMockRestServiceServer
  @SpringBootTest
  public class CollectorApplicationTest {
      
      @Autowired
      MockRestServiceServer server;
      
      @Value(&quot;${components.web-admin-portal.rest.schemas}&quot;)
      String webAdminPortal;
      
      @Before
      public void init() {
          
          server.expect(MockRestRequestMatchers.requestTo(webAdminPortal))
                .andRespond(withSuccess(&quot;{}&quot;, MediaType.APPLICATION_JSON));
      }
      
      @Test
      public void contextLoads() {
          
      }
  }
  </code></pre>
  <p>But the context loads before the <code>@Before</code> method is executed and fails with a message saying the <code>MockRestServiceServer</code> didn't expect the request.</p>
  <p>Then I tried using <code>ApplicationContextInitializer</code>:</p>
  <pre><code>import org.springframework.context.ApplicationContextInitializer;
  import org.springframework.context.ConfigurableApplicationContext;
  import org.springframework.http.MediaType;
  import org.springframework.test.web.client.MockRestServiceServer;
  import org.springframework.test.web.client.match.MockRestRequestMatchers;
  
  import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
  
  public class AppInit implements ApplicationContextInitializer&lt;ConfigurableApplicationContext&gt; {
      
      @Override
      public void initialize(final ConfigurableApplicationContext context) {
          
          context.refresh();
          MockRestServiceServer server         = context.getBean(MockRestServiceServer.class);
          String                webAdminPortal = context.getEnvironment()
                                                        .getProperty(&quot;components.web-admin-portal.rest.schemas&quot;);
          
          server.expect(MockRestRequestMatchers.requestTo(webAdminPortal))
                .andRespond(withSuccess(&quot;{}&quot;, MediaType.APPLICATION_JSON));
      }
  }
  </code></pre>
  <p>But then it complains that the <code>MockServerRestTemplateCustomizer has not been bound to a RestTemplate</code>. I figured that this issue may be resoled by using <code>@RestClientTest</code> annotation on the test class since it would disable auto configuration of <code>RestTemplateBuilder</code> and enable confiration of <code>MockRestServiceServer</code>:</p>
  <pre><code>@RunWith(SpringRunner.class)
  @SpringBootTest
  @RestClientTest
  @ContextConfiguration(initializers = AppInit.class)
  public class CollectorApplicationTest {
  </code></pre>
  <p>But it did not change anything.</p>
  <p>Thanks in advance.</p>