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.
HTML client talks to BFF API which in-turn talks to backend API
- Spring boot service in Kotlin
- Specmatic Enterprise
- Docker Desktop (to run contract test and mock servers using test containers)
- Json Query Language for specmatic-stub hook script
Our contract test will start the specmatic mock 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/test/html/index.html
For Windows Command Prompt:
gradlew test --tests="com.component.orders.contract.ContractTests"
Note: If you are using Docker Desktop, ensure that Networking is set to use 'host' mode.
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:
./gradlew test --tests="com.component.orders.contract.ContractTestUsingCLITest"
For Windows Command Prompt:
gradlew test --tests="com.component.orders.contract.ContractTestUsingCLITest"
For Unix based systems and Windows Powershell, execute the following commands in separate terminals:
./gradlew bootRun
docker run --rm --network host -v "$(pwd):/usr/src/app" specmatic/enterprise mockdocker run --rm --network host -v "$(pwd):/usr/src/app" specmatic/enterprise test
For Windows Command Prompt, execute the following commands in separate terminals:
# Start the backend service
gradlew bootRun# Start the domain api and kafka mock server docker run --rm --network host -v "%cd%:/usr/src/app" specmatic/enterprise mock
# Run contract tests docker run --rm --network host -v "%cd%:/usr/src/app" specmatic/enterprise test