rest-assured从2.1.0版本开始支持  Schema 验证,包括JSON Schema validation及Xml Schema validation。我们之前断言响应体都是一个一个字段来进行断言,这样如果断言的字段比较多的话就非常的麻烦,为了解决这个问题,我们可以使用schema文件来进行响应体的断言,schema文件可以断言整个response 。

1.JSON Schema validation

例如:在classpath下面放置以下的schema文件,products-schema.json:

 {
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Product set",
"type": "array",
"items": {
"title": "Product",
"type": "object",
"properties": {
"id": {
"description": "The unique identifier for a product",
"type": "number"
},
"name": {
"type": "string"
},
"price": {
"type": "number",
"minimum": 0,
"exclusiveMinimum": true
},
"tags": {
"type": "array",
"items": {
"type": "string"
},
"minItems": 1,
"uniqueItems": true
},
"dimensions": {
"type": "object",
"properties": {
"length": {"type": "number"},
"width": {"type": "number"},
"height": {"type": "number"}
},
"required": ["length", "width", "height"]
},
"warehouseLocation": {
"description": "Coordinates of the warehouse with the product",
"$ref": "http://json-schema.org/geo"
}
},
"required": ["id", "name", "price"]
}
}

我们可以通过上面的schema文件来验证 "/products" 这个请求的响应数据是否符合规范:

 get("/products").then().assertThat().body(matchesJsonSchemaInClasspath("products-schema.json"));

matchesJsonSchemaInClasspath 是从 io.restassured.module.jsv.JsonSchemaValidator 这个类中静态导入的,并且推荐静态导入这个类中的所有方法,然而为了能够使用io.restassured.module.jsv.JsonSchemaValidator 这个类必须依赖 json-schema-validator module ,我们可以从这个网页下载它,或者是通过maven添加以下依赖来获取:

<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-schema-validator</artifactId>
<version>3.0.6</version>
</dependency>

1.1 JSON Schema Validation 设置

  rest-assured 的 json-schema-validator module 使用的是 Francis Galiegue's  json-schema-validator(fge) 库来实现验证(Validation)。如果想要配置基础 fge 库,我们可以这样写:

 // Given
JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory.newBuilder().setValidationConfiguration(ValidationConfiguration.newBuilder().setDefaultVersion(DRAFTV4).freeze()).freeze(); // When
get("/products").then().assertThat().body(matchesJsonSchemaInClasspath("products-schema.json").using(jsonSchemaFactory));

  这个 using 方法允许我们传递一个jsonSchemaFactory 实例,rest-assured在进行验证的时候就会使用这个实例。这种配置就允许我们在验证响应结果时进行更详细的配置。

  fge 库同时也允许设置validation为 checked或者unchecked,默认情况下rest-assured使用的是checked的validation,如果想要改变这个值,我们可以提供一个 JsonSchemaValidatorSettings 的实例给matcher。例如:

 get("/products").then().assertThat().body(matchesJsonSchemaInClasspath("products-schema.json").using(settings().with().checkedValidation(false)));

上面的 setting 方法是从 JsonSchemaValidatorSettings 这个类中静态导入的。

1.2  Json Schema Validation静态配置

  让我们来想象一下,假如我们想一直使用unchecked的validation并且想设置Json Schema的版本为3,与其将JsonSchemaValidatorSettings 的实例一个个提供给所有的matchers,我们不如将它定义为一个静态的:

 JsonSchemaValidator.settings = settings().with().jsonSchemaFactory(
JsonSchemaFactory.newBuilder().setValidationConfiguration(ValidationConfiguration.newBuilder().setDefaultVersion(DRAFTV3).freeze()).freeze()).
and().with().checkedValidation(false); get("/products").then().assertThat().body(matchesJsonSchemaInClasspath("products-schema.json"));

通过上面的方式,现在任意一个导入了  JsonSchemaValidator 的matcher都会使用 DRAFTV3 作为默认的版本并且使用unchecked的validation。

  为了重置 JsonSchemaValidato 为默认的配置,我们可以简单的调用一下 reset 方法来实现:

 JsonSchemaValidato.reset();

1.3 不依赖rest-assured的JSON Schema Valition

  我们不依赖rest-assured也一样可以使用 json-schema-valition ,只要我们把JSON文件表示为 String 类型,我们可以这么做:

 import org.junit.Test;
import static io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath;
import static org.hamcrest.MatcherAssert.assertThat; public class JsonSchemaValidatorWithoutRestAssuredTest { @Test public void
validates_schema_in_classpath() {
// Given
String json = ... // Greeting response // Then
assertThat(json, matchesJsonSchemaInClasspath("greeting-schema.json"));
}
}

2.Xml的 Schema 和 DTD Validation(验证)

  通过使用 Xml Schema(XSD)或者DTD我们同样可以验证Xml响应体。

XSD例子:

 get("/carRecords").then().assertThat().body(matchesXsd(xsd));

DTD例子:

 get("/videos").then().assertThat().body(matchesDtd(dtd));

matchesXsd 方法和 matchesDtd 方法属于 Hamcrest matchers 包里,我们需要静态导入 io.restassured.matcher.RestAssuredMatchers

最新文章

  1. Android下添加新的自定义键值和按键处理流程
  2. python 环境安装
  3. [持续更新]UnsatisfiedLinkError常见问题及解决方案
  4. [原]我在Windows环境下的首个Libevent测试实例
  5. [ CodeVS冲杯之路 ] P3955
  6. Inna and Sequence
  7. Swift4--函数,自学笔记
  8. J2EE进阶(十七)Hibernate中常用的HQL查询方法(getHibernateTemplate())
  9. 消除 ASP.NET Core 告警 &quot;No XML encryptor configured. Key may be persisted to storage in unencrypted form&quot;
  10. NFS网络共享文件系统
  11. mysql 记录根据日期字段倒序输出
  12. 使用apache设置绑定多个域名或网站
  13. &lt;亲测&gt;centos安装 .net core 2.1
  14. mysql分表实战
  15. oracle 中删除表 drop delete truncate
  16. vscode调试js,安装了nodejs之后还出现无法在Path上找到运行时的node
  17. Prometheus 操作符
  18. VS2010/MFC编程入门之二十二(常用控件:按钮控件Button、Radio Button和Check Box)
  19. jQuery UI基本使用方法
  20. 【cs231n】反向传播笔记

热门文章

  1. ubuntu14.04安装chromium以及flash插件
  2. 【原创】cython and python for kenlm
  3. Docker for mac安装
  4. nginx在windows平台下的使用笔记
  5. AID-应用标识符的组成规则
  6. jquery 常用工具方法
  7. sql 两大类 DDL数据定义语言 和DCL数据控制语言
  8. Java多线程设计模式(三)
  9. jmeter-性能监控(InfluxDB+Grafana)
  10. 键盘控制背景边框平滑移动(jquery)