title: Mocking/Testing HTTP Get Request
body: <p>I'm trying to write unit tests for my program and use mock data. I'm a little confused on how to intercept an HTTP Get request to a URL.</p>
  
  <p>My program calls a URL to our API and it is returned a simple XML file. I would like the test to instead of getting the XML file from the API online to receive a predetermined XML file from me so that I can compare the output to the expected output and determine if everything is working correctly.</p>
  
  <p>I was pointed to Mockito and have been seeing many different examples such as this SO post, <a href="https://stackoverflow.com/questions/10775254/how-to-use-mockito-for-testing-a-rest-service">How to use mockito for testing a REST service?</a> but it's not becoming clear to me how to set it all up and how to mock the data (i.e., return my own xml file whenever the call to the URL is made).</p>
  
  <p>The only thing I can think of is having another program made that's running locally on Tomcat and in my test pass a special URL that calls the locally running program on Tomcat and then return the xml file that I want to test with. But that just seems like overkill and I don't think that would be acceptable. Could someone please point me in the right direction.</p>
  
  <pre><code>private static InputStream getContent(String uri) {
  
      HttpURLConnection connection = null;
  
      try {
          URL url = new URL(uri);
          connection = (HttpURLConnection) url.openConnection();
          connection.setRequestMethod("GET");
          connection.setRequestProperty("Accept", "application/xml");
  
          return connection.getInputStream();
      } catch (MalformedURLException e) {
          LOGGER.error("internal error", e);
      } catch (IOException e) {
          LOGGER.error("internal error", e);
      } finally {
          if (connection != null) {
              connection.disconnect();
          }
      }
  
      return null;
  }
  </code></pre>
  
  <p>I am using Spring Boot and other parts of the Spring Framework if that helps.</p>