📐 H4 · Contract Testing with JSON Schema Validation
H4 · Contract Testing with JSON Schema: In H2 you verified each field ONE BY ONE (`title`, `severity`, ...); JSON Schema Validation verifies the ENTIRE schema you saw in F4 in ON
In H2 you verified each field ONE BY ONE (`title`, `severity`, ...); JSON Schema Validation verifies the ENTIRE schema you saw in F4 in ONE line: `matchesJsonSchemaInClasspath("bug-schema.json")`. This AUTOMATES the "contract defect" hunt from F5 — instead of manually comparing every response, you tell REST Assured "always check this" using a JSON Schema file generated from the spec. So why still use H2's individual `.body(...)` checks too, isn't schema validation ENOUGH alone? Because schema validation only checks the SHAPE (type, required field) — it does NOT check a SPECIFIC value (e.g. "title must be exactly this text"); the two are COMPLEMENTARY: schema answers "is the structure correct", individual `.body()` answers "is the content correct". **For a deep JSON Schema Validation guide → see the `/rest-assured` page.**
🎬 Automating the Hunt from F5
Manual comparison (F5)
Automatic check on every run
In F5 you found contract defects by MANUALLY comparing the spec against real responses.
The schema from F4/F6 is turned into a `bug-schema.json` file.
`matchesJsonSchemaInClasspath(...)` automatically checks this file on EVERY test run — manual comparison is now history.
From Schema to Automated Contract Testing
Put the schema into a file…
Add the JSON schema from F4/F6 to the project as a .json file.
Add the json-schema-validator dependency, import matchesJsonSchemaInClasspath.
Add to the then() chain…
Set up automatic checking on every run with .body(matchesJsonSchemaInClasspath(...)).
Order the process for setting up JSON Schema Validation.