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

Share this page
JDBC stubbing with Redis and Specmatic contract testing.

Context

To test our applications effectively on our local machines and in lower environments such as CI pipelines, we often need to isolate them from their database dependencies to make sure that the test environment is simple to set up and our tests themselves are quick to run. A few popular options that come to mind to achieve the same include in-memory databases and containerised databases. However, there are limitations to each of these approaches.

  1. In-memory DBs – They are database simulators and usually cannot handle syntax and data types that may be proprietary / vendor specific to your database. Legacy applications with stored procedures may be even harder to deal with.
  2. Containerised DBs – They are real databases and thereby also involve significant effort initially to achieve parity with your production database in terms of schema and data setup (Especially in legacy applications which lack automated DB deployment mechanisms).

To overcome these limitations we need a “Database Stub”.

Solution

Specmatic JDBC Stub is a completely wire-compatible setup that has none of the above limitations. It is able to seamlessly stub a JDBC datasource because of which your application thinks it is talking to a real database. This helps in creating a realistic test environment that can be quickly spun up in-memory.

Key features

  1. Eliminate Database Dependency: JDBC stubs allow us to isolate APIs from their database dependencies, enabling us to test without the need for a complex DB setup. By switching out the data source with a Specmatic JDBC stub, the API can communicate with the stub instead of the actual database.
  2. Define / Record and Replay Expectations: With Specmatic JDBC stub, we can set expectations for queries and their responses. These expectations can be written by hand or recorded based on actual interactions between the API and a real database. This allows you to verify that your application is only interacting with the database as intended by you.
  3. Stub vendor specific SQL syntax – Since Specmatic JDBC stub operates at the level of abstraction of a DataSource, you can effectively stub all types of interactions including stored procedure calls.
  4. Blazing Fast In-memory Setup – Super charge your tests (component, contract and API tests and more) with Specmatic’s fast in-memory JDBC stubbing capability that speeds up your overall test execution time thereby making for a great Developer Experience.

By leveraging Specmatic JDBC stubs, we can effectively test APIs, reduce dependencies, and ensure the reliability and integrity of our applications.

Check out the sample code

Sample project: Manual configuration
Sample project: Spring auto-configuration

Download Specmatic: https://github.com/znsio/specmatic/releases

Available in the Pro plan or higher

Jdbc stubing production mode jdbc stubing test mode Redis Stubbing with Specmatic Contract Testing.

Benefits of Specmatic JDBC Stub: Replacing Real Data Sources & Enhancing the Testing Process

At the core of the testing process lies the ability to isolate the application from its database dependency. Specmatic JDBC stub achieves this by replacing the real data source with a Specmatic data source. By doing so, the API under test communicates with the Specmatic JDBC stub instead of the actual database. This allows developers to run different types of tests without the hassle of depending on a complex database setup. The Specmatic JDBC stub acts as a drop in replacement for  the database in your test environments, ensuring smooth communication and accurate test results.

Setting Expectations with Specmatic JDBC Stub for Contract-Based API Testing

To effectively use Specmatic JDBC stub, the first step is to set expectations with the stub regarding the queries that it can expect and the corresponding data it needs to respond with. These expectations can be manually written or recorded based on actual interactions between the API and a real database. Once the expectations are set, the real database is no longer required in the testing setup.

Code walkthrough: Specmatic JDBC Stub for Testing

Let’s delve into a concrete example to illustrate the power of the Specmatic JDBC stub. Consider an application that provides a wrapper for managing product entities. In its production configuration, it relies on a real database for data storage.

A screen shot showcasing Specmatic's JDBC stubbing capabilities to liberate from database dependencies.

However, for testing purposes, we want to isolate the application from the actual database. By disabling the data source auto-configuration, the application loses its ability to communicate with the real database.

A code editor with JDBC Stubbing capabilities.
Using Specmatic for JDBC Stubbing to break database dependencies in a web browser screenshot.

Instead, we configure the Specmatic JDBC stub as the data source for testing. This is achieved by adding the Specmatic JDBC dependency and setting up a bean for the data source, specifying the port and directory where the expectations are stored.

Leveraging Specmatic for JDBC Stubbing in a code editor screen of Adobe Acrobat.

Seamless Contract Testing with the Specmatic JDBC Stub and OpenAPI Specification

With the Specmatic JDBC stub in place, we can now run contract tests based on the OpenAPI specification of the API. These tests make HTTP requests to the application, which in turn communicates with the Specmatic data source.

The data source responds with the predefined data that was set up as part of the expectations.

Leveraging Specmatic for JDBC Stubbing in a code editor screenshot.

When Specmatic JDBC stub receives queries that it is not expecting, it throws errors to indicate the same thereby giving us feedback when our application code is making any additional calls to the database.

A screenshot of Specmatic, a Java script editor for breaking database dependencies by leveraging JDBC stubbing.

From the application’s perspective, it appears as if it is interacting with the real database. The application processes the data and returns it to the Specmatic contract test, which then validates if the response conforms to the schema described in the OpenAPI specification.

This seamless integration allows developers to confidently test their applications while eliminating the need for an actual database.

The Benefits of Utilising Specmatic JDBC Stub for Testing Applications with Database Dependencies

In conclusion, Specmatic JDBC stub provides a powerful solution for testing applications with database dependencies. By replacing the real data source with the Specmatic data source, developers can easily isolate their applications from complex database setups in their test environments and perform comprehensive tests including API, contract tests and more. For example, with Specmatic’s ability to generate contracts from OpenAPI specifications (API specifications as contract tests), developers can verify the correctness of their APIs without the need for an actual database instance.

By leveraging Specmatic JDBC stub, developers can streamline the testing process, increase test coverage, and ensure the reliability and stability of their applications.

Sample project: Manual configuration
Sample project: Spring auto-configuration

Download Specmatic: https://github.com/znsio/specmatic/releases

Contact us to join the private beta.

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