8-9 October

Chancery Pavilion Hotel, Bengaluru

Apidays India 2025

8-9 October

Chancery Pavilion Hotel, Bengaluru

Redis Stubbing with Specmatic Contract Testing

Share this page

Check out the sample code

Sample files: https://github.com/znsio/specmatic-redis-sample
Download Specmatic: https://github.com/znsio/specmatic/releases

Available in the Pro plan or higher

Transcript

Overview – how Specmatic can be used to stub out Redis

A typical HTTP consumer would send an HTTP request to an API where it is received by a controller. Let’s say the controller needs to fetch some data using a service object, and let’s say the service object looks up the data in an instance of Redis. Redis responds with the data, the service object returns it to the controller, and the controller uses the data to formulate a response to the consumer. This is how it works in production. But in test mode, things might be a little different. Let’s say there’s an OpenAPI specification for the API, and we feed it to Specmatic to run contract tests against the API. Instead of using an actual instance of Redis, we’ll stub it out and have the contract test set expectations so that the Redis stub knows what data to return for any query sent to it by the application. Now, when we run the contract tests and the service object does a look up in Redis, it’s actually hitting the Redis stub. The Redis stub responds and the service class understands it. None the wiser that it isn’t an actual instance of Redis.

The stub is wire compatible, so no changes are needed in the service class for this to work.

Specmatic Redis stub in action

Let’s take a sample API that needs Redis to function. Here’s the StoreController class using the StoreService object to look up some data.

The StoreService object, in turn, looks up Redis. We’re now going to run contract tests against this API using its API specification. The contract tests will be generated from the specification by Specmatic. The specification from which the contract tests are generated is in a central Git repository.

You can learn more about how Specmatic runs contract tests in a previous video.

Now, let’s run the contract tests. It won’t take long. Let’s take a look at the first test. Let’s scroll down a little in the logs. We can see a test here for a GET API to fetch the description of a store with ID 1. We get this response.

This description was provided by a Redis lookup. If we scroll up a little, we can see the log from the Redis tab showing the request that it received and the response that it returned. The stub itself is wire compatible. So the same code that talks to an actual Redis instance will talk to Specmatic’s Redis stub. But how did the stub know to return this response? Let’s dig a little deeper. Just after starting up the stubbed Redis instance, we set expectations on it.

For example, when it receives a GET operation with description hyphen 1, it should return this response, and so on. In fact, we can do dependency fault injection here. The Redis stub can simulate Redis errors to test that our application responds appropriately. The Redis stub setup is self contained within the test. The stub itself starts up within a millisecond. And this is all great because this test can easily run now in CI or even just locally on the laptop.

Available in the Pro plan or higher

Related Posts

mcp auto test exposed mcp servers lying

By Yogesh Nikam

Exposed: MCP Servers Are Lying About Their Schemas

Table of Contents Practical Lessons from MCP Server Testing Over the last few weeks the Specmatic team ran a focused series of MCP server testing
Read More
specmatic genie mcp

By Jaydeep Kulkarni

Curate, Validate and Publish an MCP Server from an OpenAPI Spec with Specmatic Genie

In this walk-through we'll show exactly how we took the Postman APIs, curated the set of API end points we want to expose via MCP,
Read More
specmaticmcpdemo linkedin mcp

By Hari Krishnan

Specmatic MCP as guardrails for Coding Agents: API Spec to Full Stack implementation in minutes

Table of Contents In this walkthrough we'll show how to use Specmatic MCP server for API testing (Contract and Resiliency) and Mocking as a guardrail
Read More
arazzo openapi asyncapi demo with specmatic

By Hari Krishnan

Authoring & Leveraging Arazzo Spec for OpenAPI & AsyncAPI Workflow Testing

Seamlessly test both synchronous and asynchronous APIs in realistic workflows before they ever hit production! Discover how combining OpenAPI and AsyncAPI specs can simplify complex
Read More
Arazzo API workflow demo

By Hari Krishnan

Visual Workflow Mocking and Testing with Specmatic and Arazzo API Specifications

Table of Contents API workflow testing with Arazzo and Specmatic: Visual authoring, workflow mocking, and backend verification Here we'll walk through a practical approach to
Read More
Specmatic vs Microcks comparison

By Hari Krishnan

Specmatic vs Microcks

When evaluating tools for API testing and mocking, the choice often comes down to understanding not just what each tool can do, but how their
Read More
jaydeep aws lambda

By Jaydeep Kulkarni

AWS Lambda Data Pipeline Testing using LocalStack with Specmatic

Table of Contents Mastering Testing AWS Lambda Functions with LocalStack and Specmatic With fast-evolving data ecosystems, building reliable and scalable data products is essential. One
Read More
api days revised 1920x1080

By John

New features and a BIG Announcement! Specmatic is bringing apidays to India!

In the past few months, we have launched a flurry of exciting features, presented at several global conferences and onboarded several new team members. We
Read More
Speakers presenting on API Governance at tech conference.

Update: See how Specmatic is transforming API testing & development

We've been hard at work, rolling out exciting new features and sharing the power of Specmatic and Contract Driven Development around the globe! Let's explore
Read More

By Joel Rosario

Build Apps from API specs using AI: Self-Correcting Contract-Driven Agentic Workflows with Specmatic

Harnessing the Power of API Specifications for Robust Microservices  Modern microservice architecture hinges on precise and dependable communication between services. This is where API specifications
Read More