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

Share this page

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 popularity of microservices has revolutionized the way we think about software architecture. However, as systems grow and inter-service communication increases, the risk of integration issues can also rise. One such communication challenge appears when services interact through asynchronous messaging systems like Google Pub/Sub. To ensure reliability and adherence to predefined protocols, contract testing emerges as a crucial strategy. In this blog post, we’ll dive deeper into how Specmatic simplifies contract testing for services dependent on Google Pub/Sub, paving the way for faultless microservices communication through early identification of integration issues.

Github Repo: https://github.com/znsio/specmatic-google-pubsub-sample

See other demos of Specmatic in action with AsyncAPI:

Kafka Mocking with AsyncAPI using Specmatic
JMS Mocking with AsyncAPI using Specmatic

Understanding Google Pub/Sub and Contract Testing

Google Pub/Sub is a fully-managed real-time messaging service that allows applications to exchange messages effectively. A common scenario entails a service publishing a message to a topic, and another subscribing to that topic to receive the message. The challenge lies in ensuring that both parties agree on the message format—this is where contract testing comes into play.

Contract testing verifies interaction between service providers and consumers, confirming each side adheres to the agreed-upon contract. Specmatic, a tool adept at managing contract tests, uses the AsyncAPI specification to automate these tests using the provided schema of messages.

AsyncAPI Specifications and Specmatic

The AsyncAPI specification acts as a blueprint for your messaging API, defining the message structure, protocols, and channels used for communication. It’s what Specmatic relies on to ensure your Google Pub/Sub messages are both syntactically and functionally correct. Through the use of named examples within the AsyncAPI specification, Specmatic can publish and subscribe to messages, simulating the actions of actual services during the testing process.

Utilizing the Google Pub/Sub Emulator

Instead of interacting with a live instance of Google Pub/Sub, in this demo we opt for an emulator during testing. This approach accelerates the testing process and removes external dependencies, enabling local and CI pipeline-based executions that provide immediate feedback.

Specmatic Contract Test running as part of Github Action

Automating with Specmatic

In this demonstration of Specmatic’s capabilities, we see how Specmatic operates as an intelligent client for microservices. It sends messages and validates both the presence and correctness of the responses it receives. If requirements aren’t met, Specmatic flags the test as a failure, alerting developers to discrepancies between the service and the contract.

The Construction of a Contract Test with Specmatic

Constructing contract tests with Specmatic begins by pointing to the AsyncAPI specification within the `specmatic.json` file. Doing so allows Specmatic to generate tests automatically, fostering a seamless integration process.

The Value of Specmatic Testing

This testing approach facilitated by Specmatic offers a proactive measure for preventing integrations veering off-spec. The demonstration showcases a scenario where deliberately deviating from the AsyncAPI specification causes the contract test to fail. This illustrates Specmatic’s ability to catch errors early, preventing potential bugs from progressing down the deployment pipeline.

Specmatic’s Role in Maintaining Contract Integrity

Specmatic also ensures consistency across examples used within the AsyncAPI specification, associating messages across publish and subscribe operations via common naming conventions. This feature solidifies the link between message expectations, ensuring that changes in one area of the specification are consistently replicated throughout.

Collaborative and Efficient Development

Specmatic’s approach of treating AsyncAPI spec as the source of truth for the mutual agreement between microservice teams fosters a collaborative culture where all stakeholder teams can co-create how services interact with each other through asynchronous channels and capture the same as AsyncAPI spec in an iterative manner. This also allows for independent parallel development where teams can confidently build their respective components since Specmatic will ensure contract compatibility of services based on the AsyncAPI spec.

Conclusion

As services and their communication evolve, Specmatic’s approach to contract testing offers a scalable, efficient, and secure method for ensuring that all messages pass seamlessly between services, keeping the system in harmony. By automating contract tests utilizing the AsyncAPI specification to test Google Pub/Sub integrations, Specmatic allows developers to confidently build and maintain systems that are robust, reliable, and ready to communicate effectively. This exemplifies how automating and aligning software development practices with contract testing can vastly improve the overall software delivery lifecycle.

Related Posts

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
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,
Read More
api resiliency testing

By Naresh Jain

Why APIs Fail and How No-Code, Intelligent API Resiliency Testing Can Prevent the Next Outage

Ensuring Reliability in an API-Driven World APIs have become the backbone of today’s digital landscape, connecting applications, services, and countless user experiences. With microservices architectures
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
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

By Yogesh Nikam

Contract Testing using gRPC Specs as Executable Contracts

Transform your gRPC API specs into executable contracts in seconds Now you can easily leverage your gRPC APIs for contract testing, intelligent service virtualisation and
Read More
Redis stubing - specmatic contact development with contract testing.

Redis Stubbing with Specmatic Contract Testing

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
Read More
api genie thumb

By Naresh Jain

Achieving Seamless DevEx: API Specification and Code Generation with API Genie

Imagine turning a plain-English business requirement into a working API without writing a single line of code. That is the promise of modern natural-language driven
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
mcp auto test meet specmatic mcp auto test

By Yogesh Nikam

Testing MCP servers: How Specmatic MCP Auto-Test Catches Schema Drift and Automates Regression

We recently shared a hands-on walkthrough with Specmatic where we explored practical approaches for Testing MCP servers, and in this post we want to expand
Read More