Data Mocking: Backend? Who Needs One? Master Data Mocking in JOSF:
Since the release of JOSF 3.2.0, JOSF now comes with built-in data mocking capabilities for web-browser tests. This powerful feature allows you to simulate backend responses without needing a live server, making your test cases faster, more stable, and independent of backend availability.
What is Data Mocking?
Mocking is a technique where you replace real system interactions with simulated ones. Instead of relying on an actual backend— which might be slow, unreliable, or still in development—you can create controlled responses that your frontend will interact with during testing.
This means you can:
- Test frontend behavior without a backend.
- Simulate API responses with specific data.
- Handle edge cases (e.g., error messages, delays) with precision.
- Speed up testing by eliminating network latency.
How to start Data Mocking?
1. Setting Up Your Test Case
First, we need a test case to verify product details on a rental website.
Steps to create a test case:
- Open JOSF and create a new test case.
- Add a Web Navigation Action:
- Drag in a Navigate action.
- Set the URL to:
https://practicesoftwaretesting.com/rental
- Verify Product Name:
- Drag in a Verify (Text & Value) action.
- Add the following XPath strategy:
//div[contains(@data-test, 'product-')]//h5[@class='card-title']
- Validate the text: “Combination Pliers”
At this point, the test ensures the first product displayed is Combination Pliers. However, if the backend changes the order or removes this product, our test will fail.
2. Introducing Data Mocking
Instead of depending on backend responses, we’ll use JOSF’s Mocking feature to override the API response.
Steps to add a mock:
- Save your test case.
- Navigate to the Mocking tab.
- Click “Add Mock”.
- Restart the Web Browser (close it if it’s already running).
- Press “Start Recording”.
- Open the test browser and navigate to:
https://practicesoftwaretesting.com/rental
As the page loads, a list of API requests will appear.
3. Capturing the API Response
Now, we’ll identify the product request and replace its data.
Finding the correct request:
- In the list of requests, search for:
is_rental
. - You should see two requests:
- OPTIONS request
- GET request
- Open the GET request and check if it contains a JSON response with product details.
4. Creating a Mock Response
- Click “Create Mock” (upper-right corner).
- Name your mock: “Rental Website”.
- Click “Save”.
This will generate a new tab with the newly created mock.
Modifying the Response Data
- In the Body section of the response, locate the products JSON.
- Modify the
"name"
property to override the displayed product. - Change it to:
{ "name": "Big Excavator" }
- For more advanced mock settings, check our Advanced Mock Rules Guide.
- Save your mock and close the tab.
5. Applying the Mock in Your Test Case
Now, we’ll apply the mock inside our test case.
- Return to your test case and click on the Mocking icon.
- Click “Add Browser Network Mock”.
- Select your newly created mock (
Rental Website
). - Click “Select Products”.
- Save your test case.
6. Running the Mocked Test Case
Now that the mock is applied, let’s run the test:
- Start your test case.
- Expected Result: Your test will fail with the following error:
Expectation: [Combination Pliers] Actual: [Big Excavator]
Success! We have successfully replaced the backend API response and injected our own product data into the test.
Conclusion
With JOSF’s built-in data mocking, you no longer need a backend to test frontend behavior. You can:
✔️ Simulate API responses without waiting for backend updates.
✔️ Control test scenarios (e.g., missing data, API errors, or specific responses).
✔️ Ensure test reliability even when backend data changes.
Mocking is a powerful tool for creating stable, predictable, and lightning-fast test cases—without relying on a backend! 🚀