This sample project demonstrates how we can practice contract-driven development and contract testing in a SpringBoot (Kotlin) application that depends on an external domain service and Kafka. Here, Specmatic is used to stub calls to domain API service based on its OpenAPI spec and mock Kafka based on its AsyncAPI spec.
Here is the domain api contract/open api spec
Here is the AsyncAPI spec of Kafka that defines the topics and message schema.
- BFF: Backend for Front End
- Domain API: API managing the domain model
- Specmatic Stub/Mock Server: Create a server that can act as a real service using its OpenAPI or AsyncAPI spec
A typical web application might look like this. We can use Specmatic to practice contract-driven development and test all the components mentioned below. In this sample project, we look at how to do this for nodejs BFF which is dependent on Domain API Service and Kafka demonstrating both OpenAPI and AsyncAPI support in specmatic.
- Spring boot
- Specmatic
- Specmatic Beta extension (for mocking Kafka)
This will start the specmatic stub server for domain api and kafka mock using the information in specmatic.yaml and run contract tests using Specmatic.
- Using gradle -
./gradlew test
- Using docker -
- Start Docker Desktop
- Run the application
./gradlew bootRun
- Run the tests
docker run --network host -v "$PWD/specmatic.yaml:/usr/src/app/specmatic.yaml" -v "$PWD/build/reports/specmatic:/usr/src/app/build/reports/specmatic" znsio/specmatic test --port=8080 --host=host.docker.internal
- Docker Desktop
- Java and Gradle
- If you are on a Windows OS, please use PowerShell.
- Start domain api stub server
docker run -v "$PWD/specmatic.yaml:/usr/src/app/specmatic.yaml" -p 8090:9000 znsio/specmatic stub
- Start Kafka stub server
docker run -p 9092:9092 -p 2181:2181 -v "$PWD/specmatic.yaml:/usr/src/app/specmatic.yaml" znsio/specmatic-kafka-trial
This will start the springboot BFF server
./gradlew bootRun
Note: For Windows OS, add .exe
extension to curl command on PowerShell or use cmd.exe
instead.
curl -H "pageSize: 10" "http://localhost:8080/findAvailableProducts"
You result should look like:
[{"id":698,"name":"NUBYR","type":"book","inventory":278}]
Also observe the logs in the Specmatic HTTP Stub Server and Specmatic Kafka Mock Server.