目录
- JSON Schema 简介
 - 生成 JSON Schema 文档
 - 使用 JSON Schema 断言
 
JSON Schema 简介
- 是使用 JSON 格式编写的
 - 可以用来定义校验 JSON 数据的结构
 - 可以用来校验 JSON 数据的一致性
 - 可以用来校验 API 接口请求和响应
 
生成 JSON Schema 文档
- 复制 JSON 数据
 - 粘贴到在线生成工具中
 - 自动生成 JSON Schema 数据
 
JSON Schema 在线生成工具:https://app.quicktype.io
生成 JSON Schema 文档
预期的 JSON 文档结构
{
  "name": "Hogwarts",
  "rank": 1,
  "Courses": [
    "UI Auto Testing",
    "Java",
    "Docker",
    "JUnit5"
  ]
}
JSON Schema 响应断言
- 实战演示
 
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
import static io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath;
public class TestJsonSchema {
    @Test
    void func() {
        given()
                .header("Hello", "Hogwarts")
        .when()
                .get("https://httpbin.ceshiren.com/get")  // 发送请求
        .then()
                .log().all()  // 打印完整的响应信息
                .assertThat()
                .body(matchesJsonSchemaInClasspath("schema.json"));  // JSON Schema 断言
    }
}
附录:JSON Schema 依赖配置
- JSON Schema Validator
 - 在 
pom.xml中添加配置信息 
<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>json-schema-validator</artifactId>
    <version>4.4.0</version>
    <scope>test</scope>
</dependency>
        
