How does OutMock work?
OutMock allows you to create a fake or 'mock' implementation of your external REST API's that can be programmed to behave the way you like. In Service Center (only on dev) you then configure your application to no longer point to the real API endpoint, but to call the mock endpoint instead.
During normal operation, all requests to this mock will forward the requests to the actual, real API.
During tests (when the mock receives requests with the header "X-Test-Id") the mock will not forward to the real API but will only respond with preconfigured responses that you can program from within your tests.
In your test you can then program the mock to respond with specific data given a specific request, before calling your application code that will call the external API.
This would, for example, look something like this:
Setting up your mock
To set up your mock, create a Service module that exposes a REST API with the same endpoints as the API you want to mock.
So for each endpoint (path) on your real API, add an endpoint on your mock.
Don't worry about query parameters or request body, they will be mapped automatically. There is no need to define them in your mock. Just focus on the Path, the path parameters and the HTTP method (GET, POST, ...). Return your response as Text. No need to map it to a structure.
Inside of each endpoint of your mock, call OutMock's MockRequest server action. That will handle all the mocking and forwarding for you. Just return the response as Text.
In the demo, in module ProductsAPI_Mock this looks like this:
Notice that as inputs only the path parameters are mentioned.
As output, all responses are of type Text.
In each endpoint, call MockRequest, like this:
Now for testing convenience, create the following Service Actions for each endpoint:
Example from the demo:
Adapting your application to pass X-Test-Id
In order for the mock to know whether or not it is running inside a BDD Test, you need to send the header "X-Test-Id" on outgoing requests to the mock. You can do that in the OnBefore of your _IS module that calls the external REST API, like so:
See OnBeforeRequest in Products_IS in the demo:
Testing using the mock
Check the OutMock Demo - BDD Tests Forge component for an example on how to test the demo.
It shows how to set a TestId on CallContext, so that mocking is activated. And how to prepare in the given steps and verify the mock in the then steps in the BDD framework.