Redis Stubbing with Specmatic Contract Testing

Share this page
Redis stubing - specmatic contact development with contract testing.

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

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 key component of many modern data architectures is AWS Lambda, which offers serverless compute power to process data streams efficiently. However, testing these Lambda functions within a data pipeline can…
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 are bringing apidays to India! This is all part of our mission to help organizations worldwide build more reliable, maintainable, and future-ready APIs.  Apidays India 2025: Future-Proof APIs for Billions! …
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 the latest developments in how Specmatic is transforming API testing & development. Elevate Your Impact - with Specmatic  Point, Click, Generate, Validate & Test: Your OpenAPI Examples Just Got Smarter …
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 serve as the linchpin, establishing clear, executable contracts that dictate how services interact. With advancements in AI, we can now take these specifications and seamlessly transform them into running applications.…
Read more

OpenAPI Examples Simplified: Visualize and Generate Domain-Specific Test Data​

Streamlining API Development: An Interactive Guide to Example Generation and Validation using Specmatic  A robust, streamlined approach to API development is crucial for maintaining efficiency, reliability, and scalability in your development pipeline. One powerful methodology that has emerged is Contract-Driven Development (CDD), allowing developers to fully realize the benefits of microservices architectures. This article demonstrates…
Read more

By Hari Krishnan

Pact’s Dependency Drag​: Why Consumer-Driven Contracts Don’t Support Parallel Development

Exploring the challenges and limitations of using Pact for contract testing in a microservices environment.  In the domain of microservices, ensuring seamless communication between different services is paramount. This necessitates robust contract testing to ensure that APIs and their consumers are in sync. With an increasing number of organizations adopting microservices, tools like Pact—a consumer-driven…
Read more

By Naresh Jain

OpenAPI’s Broken Tooling: Roundtrip Fidelity Failure with CodeGen and DocGen​

Exploring the Strengths and Weaknesses of Automated API Development  Maintaining well-documented and reliable APIs is essential for any microservices development pipelines. At the heart of this process for OpenAPI specs are two important tools: CodeGen and DocGen. CodeGen, short for code generation, and DocGen, documentation generation, are designed to streamline the development cycle by automating…
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 of a seamless developer experience (DevEx) remains a constant. This article sheds light on some of the overlooked pitfalls of gRPC, a popular choice for its performance capabilities and type-safe…
Read more

By Hari Krishnan

WireMock’s Dirty Secret: Ignoring API Specs & Letting Invalid Examples Slip Through 

Overcoming the Challenges of Hand-Rolled Mocks with Contract-Driven Development  APIs and microservices have transformed the way software systems are built and maintained. However, developing a system that relies on multiple services—both internal and external—presents unique challenges, especially when some services are not fully available. In this critique of WireMock, we examine the critical importance of…
Read more
specmatic challenge – winners announced! 1536x865

By John

Specmatic Challenge – winners announced!

The Specmatic challenge is over and we are pleased to announce the winners! Congratulations to Mohd Zaid and Himanshu Singal for successfully completing the challenge and taking home the prizes! You too can experience the power of Contract Driven Development with Specmatic and transform your API specs into executable contracts in seconds. Why not give…
Read more