Contract Testing with Avro Schema files using AsyncAPI and Specmatic
Automate Contract Tests for Kafka Services Using AsyncAPI and Avro Schema
The use of Avro Schema and schema registry to an extent ensures that both consumer and provider services adhere to a mutually agreed Message Payload structure. However, message payload structure is only part of the interaction between various components in an Event Driven Architecture. There are topics / channels, interaction patterns across these topics / channels and more. This is where we need AsyncAPI specification. And AsyncAPI spec makes sure that we do not have to duplicate the schema definition and instead leverages your existing Avro Schema by allowing us to directly refer to them from your Schema Registry. By leveraging this definitive AsyncAPIspec Specmatic can ensure that all aspects of your EDA are validated through Contract Testing.
How Specmatic Tests Avro Schema
Specmatic leverages your existing Avro schema files—no need to redefine them—and references them directly within your AsyncAPI specification. This allows you to treat your AsyncAPI document as the definitive contract for all asynchronous interactions in your architecture. During testing, Specmatic acts as a simulated client or service, automatically generating tests based on the AsyncAPI spec. It fetches Avro schemas directly from your Schema Registry to serialize and deserialize messages during the test run. Specmatic verifies message flows, validates schema compliance, and asserts that all asynchronous interactions happen within the expected topics and timelines.
Benefits of Automated Avro Contract Testing with Specmatic
- Consistent Communication: Automatically checks if services strictly follow the agreed contracts, reducing integration errors caused by schema mismatches or topic misconfigurations.
- Early Defect Detection: Instantly surfaces contract violations—such as messages being sent to the wrong topic—during development, not production.
- Faster Feedback: Automates test generation and execution, saving effort and offering rapid feedback on any contract changes.
- Seamless Integration: Reuses existing Avro schema files from your registry, eliminating duplication and extra maintenance.
- Improved Collaboration: By enabling the use of AsyncAPI spec in conjunction with Avro schema, Specmatic helps us ensure all teams work with a mutually agreed API Contract.
Getting Started with Avro Testing in Specmatic
- Define AsyncAPI Specification: Make sure your spec references your Avro schemas via the Schema Registry URL.
- Connect to your Schema Registry: Specmatic pulls schemas directly, so ensure connectivity between your test environment and the registry.
- Run Specmatic in test mode: Specmatic Kafka will simulate the client/service, publishing and consuming messages based on the contract.
- Review Results: Detailed API Coverage reports, and contract validation metrics help you spot and resolve issues early.
Specmatic makes contract testing with Avro not only possible, but painless—giving you confidence that your services communicate exactly as intended.
Contract Testing with Avro Schema files using AsyncAPI and Specmatic
Automate Contract Tests for Kafka Services Using AsyncAPI and Avro Schema
The use of Avro Schema and schema registry to an extent ensures that both consumer and provider services adhere to a mutually agreed Message Payload structure. However, message payload structure is only part of the interaction between various components in an Event Driven Architecture. There are topics / channels, interaction patterns across these topics / channels and more. This is where we need AsyncAPI specification. And AsyncAPI spec makes sure that we do not have to duplicate the schema definition and instead leverages your existing Avro Schema by allowing us to directly refer to them from your Schema Registry. By leveraging this definitive AsyncAPIspec Specmatic can ensure that all aspects of your EDA are validated through Contract Testing.
How Specmatic Tests Avro Schema
Specmatic leverages your existing Avro schema files—no need to redefine them—and references them directly within your AsyncAPI specification. This allows you to treat your AsyncAPI document as the definitive contract for all asynchronous interactions in your architecture. During testing, Specmatic acts as a simulated client or service, automatically generating tests based on the AsyncAPI spec. It fetches Avro schemas directly from your Schema Registry to serialize and deserialize messages during the test run. Specmatic verifies message flows, validates schema compliance, and asserts that all asynchronous interactions happen within the expected topics and timelines.
Benefits of Automated Avro Contract Testing with Specmatic
- Consistent Communication: Automatically checks if services strictly follow the agreed contracts, reducing integration errors caused by schema mismatches or topic misconfigurations.
- Early Defect Detection: Instantly surfaces contract violations—such as messages being sent to the wrong topic—during development, not production.
- Faster Feedback: Automates test generation and execution, saving effort and offering rapid feedback on any contract changes.
- Seamless Integration: Reuses existing Avro schema files from your registry, eliminating duplication and extra maintenance.
- Improved Collaboration: By enabling the use of AsyncAPI spec in conjunction with Avro schema, Specmatic helps us ensure all teams work with a mutually agreed API Contract.
Getting Started with Avro Testing in Specmatic
- Define AsyncAPI Specification: Make sure your spec references your Avro schemas via the Schema Registry URL.
- Connect to your Schema Registry: Specmatic pulls schemas directly, so ensure connectivity between your test environment and the registry.
- Run Specmatic in test mode: Specmatic Kafka will simulate the client/service, publishing and consuming messages based on the contract.
- Review Results: Detailed API Coverage reports, and contract validation metrics help you spot and resolve issues early.
Specmatic makes contract testing with Avro not only possible, but painless—giving you confidence that your services communicate exactly as intended.