REST Assured 系列汇总 之 REST Assured 56 - JSON Schema Validation Without Rest Assured

介绍

前面我们了解了 JSON Schema Validation In Rest Assured,你也许会好奇为啥还需要在不用 REST Assured的情况下验证 JSON Schema。我们不仅要验证 JSON response schema,还需要验证 JSON request payload, 确保 payload 是否正确。如果我们是动态创建或通过外部资源获取 payload,那么在传给 request 之前,我们需要验证 shcema。

我们可能需要验证任意 JSON,并不局限于通过 Rest Assured。

我们可以独立使用 json-schema-validator,不用结合 Rest Assured。有点类似 Junit 可以集成 Selenium 或 Rest Assured 或其他类库。如果明确不用Rest Assured,就必须添加 Hamcrest (一个核心 API 和 hamcrest matcher framework 库)

前提条件

添加 json-schema-validator 依赖包

<!-- https://mvnrepository.com/artifact/io.rest-assured/json-schema-validator -->
<dependency><groupId>io.rest-assured</groupId><artifactId>json-schema-validator</artifactId><version>4.3.1</version>
</dependency>

添加 hamcrest 依赖包

<!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest -->
<dependency><groupId>org.hamcrest</groupId><artifactId>hamcrest</artifactId><version>2.1</version><scope>test</scope>
</dependency>

Validate a JSON without Rest Assured

因为 json-schema-validator 库中 schema 验证方法会创建 一个 hamcrest matcher,所以我们在不用 Rest Assured 也能验证 schema。

Sample JSON schema
将下面 JSON Schema 存在 resource 以外的文件夹下。

{"$schema": "http://json-schema.org/draft-07/schema","$id": "http://example.com/example.json","type": "object","title": "The root schema","description": "The root schema comprises the entire JSON document.","default": {},"examples": [{"firstname": "Jim","lastname": "Brown","totalprice": 111,"depositpaid": true,"bookingdates": {"checkin": "2018-01-01","checkout": "2019-01-01"},"additionalneeds": "Breakfast"}],"required": ["firstname","lastname","totalprice","depositpaid","bookingdates","additionalneeds"],"properties": {"firstname": {"$id": "#/properties/firstname","type": "string","title": "The firstname schema","description": "An explanation about the purpose of this instance.","default": "","examples": ["Jim"]},"lastname": {"$id": "#/properties/lastname","type": "string","title": "The lastname schema","description": "An explanation about the purpose of this instance.","default": "","examples": ["Brown"]},"totalprice": {"$id": "#/properties/totalprice","type": "integer","title": "The totalprice schema","description": "An explanation about the purpose of this instance.","default": 0,"examples": [111]},"depositpaid": {"$id": "#/properties/depositpaid","type": "boolean","title": "The depositpaid schema","description": "An explanation about the purpose of this instance.","default": false,"examples": [true]},"bookingdates": {"$id": "#/properties/bookingdates","type": "object","title": "The bookingdates schema","description": "An explanation about the purpose of this instance.","default": {},"examples": [{"checkin": "2018-01-01","checkout": "2019-01-01"}],"required": ["checkin","checkout"],"properties": {"checkin": {"$id": "#/properties/bookingdates/properties/checkin","type": "string","title": "The checkin schema","description": "An explanation about the purpose of this instance.","default": "","examples": ["2018-01-01"]},"checkout": {"$id": "#/properties/bookingdates/properties/checkout","type": "string","title": "The checkout schema","description": "An explanation about the purpose of this instance.","default": "","examples": ["2019-01-01"]}},"additionalProperties": true},"additionalneeds": {"$id": "#/properties/additionalneeds","type": "string","title": "The additionalneeds schema","description": "An explanation about the purpose of this instance.","default": "","examples": ["Breakfast"]}},"additionalProperties": true
}

代码:

import java.io.File;import org.hamcrest.MatcherAssert;
import org.junit.Test;import io.restassured.module.jsv.JsonSchemaValidator;public class VerifyJsonSchemaWithoutRestAssured {@Testpublic void verifyJsonSchemaWithoutRestAssured(){String json = "{\r\n" + "    \"firstname\" : \"Jim\",\r\n" + "    \"lastname\" : \"Brown\",\r\n" + "    \"totalprice\" : 111,\r\n" + "    \"depositpaid\" : true,\r\n" + "    \"bookingdates\" : {\r\n" + "        \"checkin\" : \"2018-01-01\",\r\n" + "        \"checkout\" : \"2019-01-01\"\r\n" + "    },\r\n" + "    \"additionalneeds\" : \"Breakfast\"\r\n" + "}";MatcherAssert.assertThat(json, JsonSchemaValidator.matchesJsonSchema(new File("C:\\Users\\kkk\\git\\master\\src\\test\\java\\JsonSchema\\SampleJsonSchemaCreateBooking.json")));}}

如果将期望的 JSON Schema 存在 Resource folder 下,那么也可以使用 matchesJsonSchemaInClasspath 方法。

REST Assured 56 - JSON Schema Validation Without Rest Assured相关推荐

  1. C# - JSON Schema validation

    C# - JSON Schema validation 引言 如何生成 C# 类 JSON Schema 利用在线工具 利用 Visual Studio 利用 NJsonSchema 验证 JSON ...

  2. rest-assured之Schema validation(包括JSON Schema validation及Xml Schema validation)

    rest-assured从2.1.0版本开始支持  Schema 验证,包括JSON Schema validation及Xml Schema validation.我们之前断言响应体都是一个一个字段 ...

  3. php json schema,JSON Schema Validation介绍

    工作中经常会开发一些接口公布出去,接口以HTTPHandler方式挂载在ASP.NET上,是以*.filetype的后缀形式路由到具体的处理类中.接口参数以JSON格式通过post方法写在Reques ...

  4. 3 分钟了解 JSON Schema

    大家好,我不是鱼皮. 幸运又不幸,我是一名程序员,他也是一名程序员. 周末,我在开发网站,他在开发游戏,两个人一起写代码,一起写 Bug 头秃,竟也有了一丝别样的浪漫,好不自在! 今天,他遇到了一个后 ...

  5. JSON schema(模式)

    文章目录 JSON schema 一,什么是 JSON Schema 二,定义 Schema 1) 字符串(String) 2) 数值类型 3) 对象 属性(Properties) 额外属性(Asdd ...

  6. json schema多种形式_JsonSchema使用详解

    JSON Schema是一个用于验证JSON数据结构的强大工具, 我查看并学习了JSON Schema的官方文档, 做了详细的记录, 分享一下. 我们可以使用JSON Schema在后续做接口测试中做 ...

  7. JSON Schema校验数据

    参考 JSON Schema 规范(中文版)官方网站JSON Schema 对于数据对接系统来说,接口的数据入参校验尤为重要,使用javax.validation相关注解进行校验对于java对象的关联 ...

  8. json schema多种形式_什么是JSON Schema?

    什么是JSON Schema? 如果你曾经使用过XML Schema,RelaxNG或ASN.1,那么你很可能已经知道什么是JSON Schema,并且可以跳过本文的阅读.如果你是头一次听说,或者听过 ...

  9. laravel 验证器怎么验证json对象_Postman使用tv4进行JSON Schema结构验证和断言

    JSON Scheme简介 对于JSON格式的请求数据或者响应数据,在不同的数据和场景下往往会有一部分动态的值及字段.此时我们可以使用JSON Scheme Validator(JSON结构验证)来验 ...

  10. json schema多种形式_什么是JSON Schema?及其应用方式......

    如果你曾经使用过XML Schema,RelaxNG或ASN.1,那么你很可能已经知道什么是JSON Schema,并且可以跳过本文的阅读.如果你是头一次听说,或者听过过这个词汇但不了解,那么你来对地 ...

最新文章

  1. python使用matplotlib可视化、使用xcorr函数可视化两个变量的互相关图、使用acorr函数可视化自相关图像
  2. bootstrap-fileinput组件在上传时传递额外参数
  3. Spring(一)——用Spring IOC容器创建对象
  4. 深入 char * ,char ** ,char a[ ] ,char *a[]
  5. 双11奇迹背后的大数据平台,不喧哗,自有声!
  6. C语言课后习题(46)
  7. 外媒:特斯拉正寻求扩大在华法律事务和对外关系员工队伍
  8. 优秀的项目经理都会用这60个项目管理工具模板,可直接编辑套用
  9. Jlink接口引脚定义
  10. 痴情人, 浮生梦(上)
  11. 5G可以让万人演唱会中人人有网上?有它就行 1
  12. Windows 环境变量工具 Evn
  13. Halcon找圆系列(4)测量圆直径/半径的方法之暴力拟合法 vs 测量工具法
  14. Android 集成百度身份证识别
  15. 在项目中这样写代码的时候,请搭配红花油、跌打损伤酒一起使用
  16. UE4+Cubemap(jpg导入UE4生成Cubemap)
  17. uniapp学习教程
  18. ant design 设置网页的title
  19. 2021年计算机保研(中科院网络信息中心+南开+北师大ai+北理网安+北交计院)
  20. 于博士Cadence视频教程60集全套百度网盘分享

热门文章

  1. 金莹江苏省计算机学会教授,第二届江苏省青年计算机精英论坛”在江南大学举行...
  2. 项目启动报错:Caused by: com.atomikos.recovery.LogException: Log already in use? tmlog in ./
  3. 部署asp.net mvc_在ASP.NET 2.0上部署ASP.NET MVC
  4. Python代码画哆啦A梦战斗猫--Turtle画图
  5. 一文系统搞懂协同推荐算法(二)
  6. 服务器运维大屏,可视化运维大屏
  7. 关于selenium配置Chrome驱动(Windows系统)
  8. 不要试图做完人--任正非在华为优秀党员座谈会上的发言
  9. Domain Adaptation理论分析
  10. 重装系统后如何恢复oracle10g数据…