This sample project demonstrates below aspects of Contract-Driven Development
- Contract testing a .NET core (C#) application by leveraging its OpenAPI spec to generate tests (#NOCODE)
- Service Virtualization (Stub / Mock) dependencies of this application (System Under Test), again using OpenAPI specifications of those dependency services (#NOCODE)
The Backend For Frontend (BFF) application here is the System Under Test (SUT). It depends on Domain API service which is being Stubbed out using Specmatic thereby effectively isolating the SUT so that we can now use Specmatic to run Contract Tests. Both Service Stubbing and Contract Testing is based on OpenAPI specifications here.
- .NET core (version 8)
- Specmatic
- Test Containers
- Docker Desktop (on Local) and Test Container Cloud (in CI)
- If you are on a Windows OS, please use PowerShell.
This will start the specmatic stub server for domain api using the information in specmatic.yaml and run contract tests using Specmatic.
dotnet test
- Start Docker Desktop
- Navigate to Project (
cd specmatic-order-bff-csharp
) - Run the application
dotnet run
- Navigate to test Project (
cd specmatic-order-bff-csharp.test
) - Start the stub Server
docker run -v "$PWD/specmatic.yaml:/usr/src/app/specmatic.yaml" -v "$PWD/examples:/usr/src/app/examples" -p 9000:9000 znsio/specmatic stub --examples=examples
- 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
cd specmatic-order-bff-csharp.test
docker run -v "$PWD/specmatic.yaml:/usr/src/app/specmatic.yaml" -v "$PWD/examples:/usr/src/app/examples" -p 9000:9000 znsio/specmatic stub --examples=examples
This will start the .NET core BFF server
cd specmatic-order-bff-csharp
dotnet run
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?type=gadget"
You result should look like:
[{"id":10,"name":"iPhone","type":"gadget","inventory":701}]
Note: You might not get 701 for inventory, it can return can random integer.
Also observe the logs in the Specmatic HTTP Stub Server.