Skip to content

specmatic/specmatic-order-bff-java

Repository files navigation

Specmatic Sample: Spring Boot BFF calling Domain API

Table of Contents

Background

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:

HTML client talks to BFF API which in-turn talks to backend API

Tech

  1. Spring boot service in Kotlin
  2. Specmatic
  3. Specmatic-Kafka
  4. Docker Desktop (to run contract test and mock servers using test containers)

Run Contract Tests

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.

1. Using Specmatic-JUnit Helper

For Unix based systems and Windows Powershell:

./gradlew test --tests="com.component.orders.contract.ContractTests"

View Specmatic Test Reports

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"

2. Using TestContainers

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"

3. Using Docker

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/bff

For 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

For More Info

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 12