Created
May 9, 2022 09:39
-
-
Save PramodDutta/b6eb715ef75fe55cbc63ef13c446779c to your computer and use it in GitHub Desktop.
JSON Schema Validation using Rest Assured
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package live.sdet; | |
| import io.restassured.RestAssured; | |
| import org.testng.annotations.Test; | |
| import static io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath; | |
| import static org.hamcrest.MatcherAssert.assertThat; | |
| /** | |
| * Unit test for simple App. | |
| */ | |
| public class JSonSchemaValidationRestAssured | |
| { | |
| /** | |
| * json | |
| * { | |
| * "id": 1, | |
| * "name": "Pramod", | |
| * "age": 29, | |
| * "numberInMath": 89.9 | |
| * } | |
| * | |
| * jsonSchema - https://www.jsonschema.net/app/schemas/0 | |
| * | |
| * { | |
| * "$schema": "http://json-schema.org/draft/2019-09/schema", | |
| * "$id": "http://example.com/example.json", | |
| * "title": "Root Schema", | |
| * "type": "object", | |
| * "default": {}, | |
| * "required": [ | |
| * "id", | |
| * "name", | |
| * "age", | |
| * "numberInMath" | |
| * ], | |
| * "additionalProperties": true, | |
| * "properties": { | |
| * "id": { | |
| * "title": "The id Schema", | |
| * "type": "integer", | |
| * "default": 0 | |
| * }, | |
| * "name": { | |
| * "title": "The name Schema", | |
| * "type": "string", | |
| * "default": "" | |
| * }, | |
| * "age": { | |
| * "title": "The age Schema", | |
| * "type": "integer", | |
| * "default": 0 | |
| * }, | |
| * "numberInMath": { | |
| * "title": "The numberInMath Schema", | |
| * "type": "number", | |
| * "default": 0.0 | |
| * } | |
| * } | |
| * } | |
| */ | |
| @Test | |
| public void jsonSchema() | |
| { | |
| RestAssured.baseURI = "http://localhost:8888"; | |
| String response = RestAssured.given().when().get("/data").getBody().asString(); | |
| assertThat( response, matchesJsonSchemaInClasspath("dataSchema.json")); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment