DEV Community

Anna
Anna

Posted on

Mocks, Stubs, and Contract Testing: Explanation with Examples

When testing software, especially APIs and integrations, we often need to simulate parts of the system that are unavailable or difficult to test directly. That’s where mocks, stubs, and contract testing come in. These help testers and developers ensure everything works without relying on external systems every time.

*What is Testing with Stubs? *

A stub is used to replace a real object. It only simulates specific behavior rather than the entire object. This approach is useful when your tests interact with only certain aspects of an object's behavior.

Example:

Imagine you're testing an online store’s checkout process. The payment system isn’t ready yet, but you must test order placement.

Without a stub: The test fails because there’s no real payment system.

With a stub: You create a function that always returns "payment successful" when called, letting you test order processing without a real payment gateway.

📌 Key point: Stubs return hardcoded responses.

What is Testing with Mocks?

A mock is like a stub but smarter. It doesn’t just return fixed data it also checks if it was used correctly. Mocks help verify interactions between components.

Example:

You're testing an email notification system that should send an email when an order is placed.

Without a mock: You would need to check your real inbox.

With a mock: You create a fake email service that records whether the email function was called with the correct details (e.g., recipient, subject).

📌 Key point: Mocks track and validate function calls, not just return data.

*Contract Testing – Ensuring API compatibility *

Contract testing ensures that two systems (e.g., frontend and backend) communicate correctly by verifying that the API responses meet the expected contract.

Example:

A mobile app fetches user data from a backend API. The API team updates the backend, but before deploying, they want to ensure the update won’t break the app.

With contract testing: The API team runs a test to confirm that the new API responses still match the expected structure the mobile app uses.

📌 Key point: Contract testing ensures that changes in one system don’t break another.

*Using Mocks and Stubs in Contract Testing *

Mocks and stubs are crucial in simulating real API behavior and validating the expected request-response structure without relying on a live service.

🔹 How Stubs Help in Contract Testing:

Act as fake API responses for a consumer.

Allow testing of the frontend/backend against predetermined responses.

Ensure consumer-side contract validation without calling the real API.

🔹 How Mocks Help in Contract Testing:

Ensure the consumer sends requests correctly (method, headers, request body).

Verify that the provider (API) meets the expected contract.

Useful for testing provider-side contracts without a real consumer.

*How They Work Together in Contract Testing *

Consumer-side contract testing (Frontend or another service):

Uses stubs to simulate API responses.

Generates a contract that defines the expected API response.

Provider-side contract testing (Backend API):

Uses mocks to validate that it adheres to the contract.

Ensures changes in the API do not break consumer expectations.

By combining stubs (for consuming services) and mocks (for providing services), teams can independently test their components while maintaining integration integrity.

Top comments (0)