- in production, object A's method a() calls object B's method b()
- in test, we
- pre-define b()'s behaviour
- inject mocked object B into object-under-test A
- fire a() and verify the output
But if we have a real HTTP server somewhere, in order to mock response from remote, all we need is to set up endpoint with the response and later tear it down. As a side-product, caching service is such a HTTP server that can easily be used to mock remote APIs object-under-test consume. It works this way
- in product, object A's method a() send HTTP request to service B's endpoint b
- in test, we
- pre-populate (POST) expected response in cache server for endpoint b
- send (GET) HTTP request to it (instead of the production endpoint) from A.a()
- get response from cache server and verify a()'s output
- remove (DELETE) the response from cache server
Happy testing!