Say that Java application makes requests to http://www.google.com/...
and there's no way to configure the inherited library (making such requests internally), so I can not stub or replace this URL.
Please, share some best practices to create a mock like
whenCalling("http://www.google.com/some/path").withMethod("GET").thenExpectResponse("HELLO")
so a request made by any HTTP client to this URL would be redirected to the mock and replaced with this response "HELLO"
in the context of current JVM process.
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.
Could you show a simple set up from the main
method like:
- create mock
- start mock simulation
- make a request to the URL by an arbitrary HTTP client (not entangled with the mocking library)
- receive mocked response
- stop mock simulation
- make the same request as on step 3
- receive real response from URL
Here's how to achieve what you want with the API Simulator.
The example demonstrates two different ways to configure Embedded API Simulator as HTTP proxy for the Spring's RestTemplate client. Check with the documentation of the (quote from the question) "inherited library" - often times Java-based clients rely on system properties described here or may offer some way to configure HTTP proxy with code.
When using maven, add the following to project's
pom.xml
to include the Embedded API Simulator as a dependency:... and this to point to the repository: