Table of Contents
In this sample project, we will use Specmatic to contract test the BFF (Backend for Frontend) in isolation. BFF is dependent on Domain API and Kafka. Using this sample project we'll demonstrate both OpenAPI and AsyncAPI support in Specmatic.
Following are the specifications used in this project:
- BFF's OpenAPI spec is used for running contract tests against the BFF.
- Domain API's OpenAPI spec is used for stubbing the Domain API.
- AsyncAPI spec of Kafka that defines the topics and message schema and is used for mocking interactions with Kafka.
- Spring boot service in Kotlin
- Specmatic
- Specmatic-Kafka
- Docker Desktop (to run contract test and mock servers using test containers)
Our contract test will start the Specmatic stub server for Domain API and Kafka mock using specmatic config and run contract tests against the BFF using Specmatic.
For Unix based systems and Windows Powershell:
./gradlew test --tests="com.component.orders.contract.ContractTests"After running the contract tests, you can view the test reports generated by Specmatic in the following location: build/reports/specmatic/index.html
For Windows Command Prompt:
gradlew test --tests="com.component.orders.contract.ContractTests"For Unix based systems and Windows Powershell:
./gradlew test --tests="com.component.orders.contract.ContractTestsUsingTestContainer"For Windows Command Prompt:
gradlew test --tests="com.component.orders.contract.ContractTestsUsingTestContainer"For Unix based systems and Windows Powershell, execute the following commands in separate terminals:
# Start the backend service
./gradlew bootRun# Start the domain api mock server
docker run --rm -p 8090:9000 -v "$(pwd)/src/test/resources/specmatic.yaml:/usr/src/app/specmatic.yaml" -v "$(pwd)/src/test/resources/domain_service:/usr/src/app/domain_service" specmatic/specmatic virtualize --examples /usr/src/app/domain_service# Start the kafka mock server
docker run --rm -p 9092:9092 -p 2181:2181 -v "$(pwd)/src/test/resources/specmatic.yaml:/usr/src/app/specmatic.yaml" specmatic/specmatic-kafka virtualize# Run contract tests
docker run --rm --network host -v "$(pwd)/src/test/resources/specmatic.yaml:/usr/src/app/specmatic.yaml" -v "$(pwd)/src/test/resources/bff:/usr/src/app/bff" -v "$(pwd)/build/reports/specmatic:/usr/src/app/build/reports/specmatic" specmatic/specmatic test --port=8080 --examples /usr/src/app/bffFor Windows Command Prompt, execute the following commands in separate terminals:
# Start the backend service
gradlew bootRun# Start the domain api mock server
docker run --rm -p 8090:9000 -v "%cd%/src/test/resources/specmatic.yaml:/usr/src/app/specmatic.yaml" -v "%cd%/src/test/resources/domain_service:/usr/src/app/domain_service" specmatic/specmatic virtualize --examples /usr/src/app/domain_service# Start the kafka mock server
docker run --rm -p 9092:9092 -p 2181:2181 -v "%cd%/src/test/resources/specmatic.yaml:/usr/src/app/specmatic.yaml" specmatic/specmatic-kafka virtualize# Run contract tests
docker run --rm --network host -v "%cd%/src/test/resources/specmatic.yaml:/usr/src/app/specmatic.yaml" -v "%cd%/src/test/resources/bff:/usr/src/app/bff" -v "%cd%/build/reports/specmatic:/usr/src/app/build/reports/specmatic" specmatic/specmatic test --port=8080 --examples /usr/src/app/bff