Skip to content

Latest commit

 

History

History
88 lines (62 loc) · 2.64 KB

File metadata and controls

88 lines (62 loc) · 2.64 KB

Specmatic Lab: Dictionary to Generate Domain-Aware Request/Response Payloads

Objective

Understand how you can use Specmatic Dictionary feature to generate deterministic, domain-aware request payloads from contract testing and response from service virtualization.

Time required to complete this lab

10-15 minutes.

Prerequisites

  • Docker is installed and running.
  • You are in labs/dictionary.

Files in this lab

  • .specmatic/repos/labs-contracts/openapi/dictionary/simple-openapi-spec.yaml - runtime OpenAPI contract used by specmatic.yaml after the contracts repo is checked out.
  • specs/simple-openapi-spec.yaml - local copy kept only for the examples dictionary --spec-file ... command in this lab.
  • examples/*.json - External examples used by tests.
  • specmatic.yaml - Specmatic config file that pulls the runtime contract from labs-contracts and uses local examples.

1. Run the suite using Docker (intentional failure)

docker compose up suite --abort-on-container-exit

Expected output:

Tests run: 3, Successes: 0, Failures: 3, Errors: 0

Root cause of the failed tests

Tests expect specific response values, but the mock generates random valid values.
We will fix this by configuring dictionary-driven mock generation, not by editing the example test files.

Clean up before the next step:

docker compose down -v

2. Learner task: configure dictionary-based mock data

Generate dictionary data from existing examples:

docker run --rm -v .:/usr/src/app specmatic/enterprise examples dictionary --examples-dir examples --spec-file specs/simple-openapi-spec.yaml --out specs/dictionary.yaml
Generated dictionary file at specs/dictionary.yaml

Open and understand the generated dictionary file

Update specmatic.yaml under dependencies.services[0].service to add:

data:
  dictionary:
    path: specs/dictionary.yaml

3. Re-run the suite after configuring dictionary

docker compose up suite --abort-on-container-exit

Expected output:

Tests run: 3, Successes: 3, Failures: 0, Errors: 0

Clean up:

docker compose down -v

Pass Criteria

  • Baseline run fails with Tests run: 3, Successes: 0, Failures: 3, Errors: 0.
  • After dictionary configuration, run passes with Tests run: 3, Successes: 3, Failures: 0, Errors: 0.

Additional Resources

Next step

If you are doing this lab as part of an eLearning course, return to the eLearning site and continue with the next module.