title: How to mock the webservice response of RestTemplate?
body: <p>I'd like to write an integration test on my whole application, and want to mock only one specific method: the <code>RestTemplate</code> that I use to send some data to an external webservice and receive the response.</p>
  
  <p>I want to read the response instead from a local file (to mock and mimic the external server response, so it is always the same).</p>
  
  <p>My local file should just contain the<code>json/xml</code> response that in production the external webserver would respond with.</p>
  
  <p>Question: how can I mock an external xml response?</p>
  
  <pre><code>@Service
  public class MyBusinessClient {
        @Autowired
        private RestTemplate template;
  
        public ResponseEntity&lt;ProductsResponse&gt; send(Req req) {
                 //sends request to external webservice api
                 return template.postForEntity(host, req, ProductsResponse.class);
        }
  }
  
  @RunWith(SpringRunner.class)
  @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
  public class Test {
  
     @Test
     public void test() {
          String xml = loadFromFile("productsResponse.xml");
          //TODO how can I tell RestTemplate to assume that the external webserver responded with the value in xml variable?
     }
  }
  </code></pre>