Intro-to-SV
Last updated
Last updated
Service virtualization, Fakes, Stubs, Mocks etc are all different categories of Test Doubles which on broader level return a response for a given request in a controlled manner.
Let’s dive more into the concept of API mocking and some of the scenarios where they can be useful.
Mock is nothing but an imitation. It simulates behaviour of real API but in a more a controlled manner. A simple mock server consists of a server, which on matching certain request returns pre-defined response and other parameters associated such as response code etc.
These are just some of the scenarios where API Mocks can be useful. It again depends upon the project requirement.
Mocking during development:
Developers are always working on writing code that integrates with other system components via APIs. It might not always be desirable or even possible to actually access those systems during development. There could be security, performance or maintenance issues that make them unavailable , or they might simply not have been developed yet. This is where mocking comes in: instead of developing code with actual external dependencies in place, a mock of those dependencies is created and used instead. Depending on your development needs this mock is made “intelligent” enough to allow you to make the calls you need and get similar results back as you would from the actual component, thus enabling development to move forward.
Mocking for Functional Testing and Non-Functional Testing:
The testing usage of mocking is similar to development. Components may be unavailable for security, performance, maintenance or non-existence reasons. A mock of the component to be tested can be more than enough to get the testing started. Non-functional testing aims at making sure that your component or system under test behaves correctly in regards to performance, SLAs, security etc. For example, you might want to make sure that if you get an error from your back-end system that it will be logged correctly, or that your component gracefully handles situations like when a 3rd party API stops responding during a load test etc.
Mocking for External Components:
When doing functional testing of a component which depends on external components accessed via APIs is a situation where mocking can be very useful. For example, you might need to test a geo-location functionality in your component that in turn uses Google Maps APIs. Instead of using the actual Google Maps API you could use a mock that would return known results to your component. This helps you validate that the results from your component are returned correctly. An added benefit is that you keep your usage of external APIs to a minimum.
Mocking for Demonstration:
Sometimes an API needs to be made available to consumers before they can try them out before committing to use them. In this case, a complete simulation of the API can be provided, online or as a distributable for local deployment. The consumer can try out API from their system, and make basic requirement assessment without imposing any cost on the actual API.
API mocking is a powerful concept that can be put to use in several development and testing scenarios.
Now, Let's get started with Project Creation for SV. Click Here to see how to Create SV Projects.