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

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

API Resiliency and Contract Testing for GraphQL

Transform your GraphQL API specs into executable contracts in seconds Now you can easily leverage your GraphQL APIs for contract testing, intelligent service virtualisation and
Read More
testing 202 responses thumb

By Naresh Jain

When Downstream Services Lag, Does Your API Gracefully Accept with 202 Responses?

When Downstream Services Lag: Designing Reliable APIs with 202 responses As systems get distributed, synchronous calls to downstream services become fragile. When a downstream service
Read More

By Jaydeep Kulkarni

JMS Mocking with AsyncAPI using Specmatic

The JMS mock is wire compatible and can be controlled entirely from within the test. This means you can run the test locally or also
Read More

Contract Testing Google Pub/Sub: Using AsyncAPI specs as Executable Contracts

Shift-Left the identification of integration issues in applications built with Event Driven Architecture (EDA) by leveraging AsyncAPI specs as Executable Contracts Introduction The surge in
Read More
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
api proxy recording thumb

By Naresh Jain

Replace Live Services with OpenAPI Mocks from Real HTTP Traffic with Specmatic Proxy

API proxy recording: Capture traffic, generate mocks, and simulate faults When you need to test how a system behaves when a downstream API misbehaves, API
Read More
JDBC stubbing with Redis and Specmatic contract testing.

Break the Chains of Database Dependencies: Leveraging Specmatic for JDBC Stubbing

With Specmatic JDBC stub, you can easily test APIs without the need for a complex database setup. By switching out the real database with a
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

By Naresh Jain

gRPC Flaws​ – The Illusion of Safety & Frustrating DevEx in Proto3’s Type-Safe Contracts​

Understanding the Shortcomings of gRPC and How Contract Testing Can Bridge the Gap  In the ever-evolving world of API design, development, and testing, the pursuit
Read More