This guide covers the core concepts of API Virtualization (Service Mocking) and provides a step-by-step example of how to mock a simple, static response.
What is API Virtualization?
API Virtualization is the practice of creating a "mock" or "dummy" version of a REST API. This is incredibly useful for testing your application when a real backend service is unavailable, unstable, or not yet developed. It allows you to simulate any scenario, test in isolation, and reduce dependencies during development. The basic workflow in ACCELQ is to Define a rule, Enable it, run your test, and then Disable it.
Example: Mocking a User Details API
Let's imagine the endpoint GET /api/users/profile/123 is down for maintenance, but we still need to test a feature that relies on it. We can create a virtual service that returns a predictable, successful response.
Step 1: Define the Virtualization Rule
The first step is to define a rule that intercepts the call to the user profile endpoint and provides a mock response.
- Command:
Define Virtualization rule - Details:
ref name:mockUserProfile(A unique name for this rule)group:user-services(To categorize related rules)Base URL:https://api.myapp.commethod type:GETendpoint:equals to/api/users/profile/123Mock response status:200Mock response body:{"id": 123, "username": "testuser", "email": "test@example.com", "active": true}response body content type:application/json
Step 2: Enable the Rule
A defined rule is inactive until you enable it. We will enable the specific rule we just created.
- Command:
Enable virtualization rules Example Logic:
Enable virtualization rule with ref name:
mockUserProfileof group with reference name:user-services
Step 3: Use the Mocked Service
Now that the rule is active, any API call made from your test that matches the defined criteria will be intercepted.
Example Logic:
Invoke ReSTful GET service. End-point:
https://api.myapp.com/api/users/profile/123
Instead of this call going to the real (and currently unavailable) server, ACCELQ's virtualization engine will intercept it and immediately return the mock 200 response we defined in Step 1.
Step 4: Disable and Clean Up
After your test steps are complete, it's good practice to clean up by disabling the rule.
- Command:
Disable virtualization rules - Purpose: This makes the rule inactive, so subsequent calls will go to the real service again. You can also use
Delete virtualization rulesto remove the definition entirely.
Comments
0 comments
Please sign in to leave a comment.