title: How to capture HTTP request and mock its response in Java?
body: <p>Say that Java application makes requests to <code>http://www.google.com/...</code> and there's no way to configure the inherited library (making such requests internally), so I can not stub or replace this URL.</p>
  
  <p>Please, share some best practices to create a mock like</p>
  
  <p><code>whenCalling("http://www.google.com/some/path").withMethod("GET").thenExpectResponse("HELLO")</code></p>
  
  <p>so a request made by any HTTP client to this URL would be redirected to the mock and replaced with this response <code>"HELLO"</code> in the context of current JVM process.</p>
  
  <p>I tried to find a solution using WireMock, Mockito or Hoverfly, but it seems that they do something different from that. Probably I just failed to use them properly. </p>
  
  <p>Could you show a simple set up from the <code>main</code> method like:</p>
  
  <ol>
  <li>create mock</li>
  <li>start mock simulation</li>
  <li>make a request to the URL by an arbitrary HTTP client (not entangled with the mocking library)</li>
  <li>receive mocked response</li>
  <li>stop mock simulation</li>
  <li>make the same request as on step 3</li>
  <li>receive real response from URL</li>
  </ol>