Mock servers for integration testing
Team A is developing a new login feature for an application including a two-factor authentication feature. This two-factor authentication feature is being built by an external team, but this is not finished yet. Team A is not able to complete testing because of this delay in integration, they want to mitigate the risk of any part of the code dealing with two-factor authentications.
I’ve come across similar situations many times before. But often, the team just accepts the risk of this situation and move on. What if we can mitigate this problem. Solving these kinds of problems brings more predictability and stability to the planning. Therefore, automation tests will be done on time. Delaying the integration tests when the team has moved on can cause more impact due to change in context, as well as an increase in the cost of bugs.
One way to mitigate this risk is to setup mock servers to simulate the actual service calls. Postman allows us to set it up free (up to 1000 mock requests). This blog will take you through how to set up a mock server using Postman.
In short: Create a mock server using Postman. This will simulate the challenge-response scenarios. (for example a. Success, b. Failure, c. Not Allowed, d. Error). Add the mock server endpoint to the development and testing environment configurations. Test the integration with the challenge configured for all scenarios (a, b, c, d). Make dynamic adjustments to the mock request responses as per the changes.
Start with a new request in Postman, save this to a new collection that says “Mock Collection”. It will look like this:
Select examples – You can find these in the environment drop-down menu. Then select “add example”.
Enter a name for the example which matches with our scenario. Enter the correct status code for the scenario and any response if applicable and save the example. You can also set the content-type to application/JSON in the header section of the response (for my example). Then create more to match all required scenarios.
Now, let’s create the mock server. Click the arrow on your collection and go to the Mock tab. Then, click on “create mock server”. Now you will need to enter a name for your mock server environment. If you don’t want to use any environment variables for your mock server, select “no environment”. Next, you will receive a URL for your mock server. (You can set the server to private. To do this, you have to put an API key in the header)
Now you can construct a request using the mock server URL and check if they are working as expected:
Voila! They’re working as expected. Now let’s give the developers the mock server URL and ask them to use them in development and testing environments to simulate different cases. You can test scenarios by supplying relevant challenge codes (1000 for success, 2000 for failure, 3000 for not authorized and 4000 for internal server error)
This way you can simulate all the possible scenarios in an external API that can report to you. You can create or modify your responses according to the changes in the requirements during the development as well.
If you’re already using SoapUI in your project, they provide SOAP service mocking in addition to REST mocking:
- Rest Services: https://www.soapui.org/rest-testing-mocking/service-mocking-overview.html
- Soap Services: https://www.soapui.org/soap-mocking/service-mocking-overview.html
Postman is a REST client, while we can make it work for SOAP (See here) SoapUI is better off for SOAP services.
😊 Happy testing!