This lab shows why async contract testing matters in real teams: producers and consumers often deploy independently, and schema drift in event payloads can break workflows silently. Here, the AsyncAPI contract is the shared source of truth, and Specmatic catches drift before release.
Run an async contract test against a real Kafka-based provider, observe the intentional failure, fix the provider implementation, and verify a passing run.
10-15 minutes (first run can take longer if Docker images are not cached locally).
- Docker Desktop (or Docker Engine + Compose v2) is installed and running.
- You are in
labs/quick-start-async-contract-testing. - Port
9092is free (required for Kafka in this lab). - Ports
9000and9001are free (required for Studio).
.specmatic/repos/labs-contracts/asyncapi/quick-start/async.yaml- AsyncAPI contract (source of truth for this lab)service/processor.py- Provider implementation with one intentional mismatchspecmatic.yaml- Specmatic async test configurationdocker-compose.yaml- Kafka, provider service, test runner, and optional Studiocreate-topics.sh- Kafka topic bootstrap script used bykafka-init
Fix the provider so the emitted response event matches the contract's allowed status values.
- Do not edit: the specs in
.specmatic/repos/labs-contracts/asyncapi/quick-start/async.yaml - Do not edit:
specmatic.yaml,docker-compose.yaml. - Edit only:
service/processor.py. - If your baseline run unexpectedly passes, reset
service/processor.pysoprocess_messagereturns"status": "STARTED"before continuing.
- Consumer under test: Specmatic test runner (
contract-testservice). - Provider under test: Python processor (
providerservice). - Flow:
- Specmatic publishes an event/message to Kafka topic
new-orders. - Provider consumes that event, transforms payload, and emits to
wip-orders. - Specmatic validates emitted payload and headers against
.specmatic/repos/labs-contracts/asyncapi/quick-start/async.yaml.
- Specmatic publishes an event/message to Kafka topic
From this folder, run:
docker compose up contract-test --build --abort-on-container-exitExpected failure signal:
- Contract test fails due to a mismatch in the provider's response event payload.
- Failure points to
status, where actual is"STARTED"and expected is one of the enum values in the contract (including"INITIATED").
Tests run: 1, Successes: 0, Failures: 1, Errors: 0
Then clean up:
docker compose down -vOpen service/processor.py and update process_message:
From:
"status": "STARTED",To:
"status": "INITIATED",Re-run:
docker compose up contract-test --build --abort-on-container-exitExpected pass signal:
Tests run: 1, Successes: 1, Failures: 0, Errors: 0
Then clean up:
docker compose down -vStart Studio:
docker compose --profile studio up studio --buildOpen Studio, load specmatic.yaml, and click Run Suite.
Also inspect the loaded contract in the left sidebar by opening .specmatic/repos/labs-contracts/asyncapi/quick-start/async.yaml.
Stop Studio stack:
docker compose --profile studio down -vport is already allocated:- Free
9092,9000, and9001, then retry.
- Free
kafka-initfails with shell/script errors on Windows:- Ensure shell scripts are checked out with LF line endings (
create-topics.shmust not contain CRLF).
- Ensure shell scripts are checked out with LF line endings (
- Test exits before provider is ready:
- Re-run once; services are health-gated, but first image pull/startup can be slow.
- Async contract testing validates event-driven behavior against AsyncAPI contracts.
- Contract failures provide precise mismatch diagnostics (rule code + field path).
- Keeping the contract stable while fixing provider behavior is a practical producer-side workflow.
- Specmatic gives actionable mismatch feedback for event payloads and headers.
If you are doing this lab as part of an eLearning course, return to the eLearning site and continue with the next module.