java - 使用 REST Assured,我如何验证 Json 对象数组中的数据类型
问题描述
我是 Rest Assured 的新手,并尝试使用 json-schema-validator version-4.3.0 进行一些验证。运行时出错。在这方面需要帮助。
这是我的架构(sample.json):
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"title": {
"type": "string"
},
"subTitle": {
"type": "string"
},
"author": {
"type": "string"
},
"publish_date": {
"type": "string"
},
"publisher": {
"type": "string"
},
"pages": {
"type": "number"
},
"Description": {
"type": "string"
},
"Website": {
"type": "string"
}
},
"required": ["title", "subTitle", "author", "publish_date", "publisher", "pages", "Description", "Website"]
}
我的 json 响应是这样的:
{
"books": [
{
"isbn": "9781449325862",
"title": "Git Pocket Guide",
"subTitle": "A Working Introduction",
"author": "Richard E. Silverman",
"publish_date": "2020-06-04T08:48:39.000Z",
"publisher": "O'Reilly Media",
"pages": 234,
"description": "This pocket guide is the perfect on-the-job companion to Git, the distributed version control system. It provides a compact, readable introduction to Git for new users, as well as a reference to common commands and procedures for those of you with Git exp",
"website": "http://chimera.labs.oreilly.com/books/1230000000561/index.html"
}
}
编写的测试用例是:
public class AppTest {
@Test
public void verifyJsonSchema() {
// GIVEN
RestAssured
.given()
.baseUri("https://bookstore.toolsqa.com/BookStore/v1/Books/9781449325862")
// WHEN
.when()
.get()
// THEN
.then()
.assertThat()
.statusCode(200)
.body(JsonSchemaValidator.matchesJsonSchemaInClasspath("sample.json"));
}
}
我/我得到的错误是:
[RemoteTestNG] detected TestNG version 6.14.3
FAILED: verifyJsonSchema
io.restassured.module.jsv.JsonSchemaValidationException: com.fasterxml.jackson.core.JsonParseException: Unexpected character ('<' (code 60)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
at [Source: java.io.StringReader@56febdc; line: 2, column: 2]
at io.restassured.module.jsv.JsonSchemaValidator.matchesSafely(JsonSchemaValidator.java:233)
at io.restassured.module.jsv.JsonSchemaValidator.matchesSafely(JsonSchemaValidator.java:75)
at org.hamcrest.TypeSafeMatcher.matches(TypeSafeMatcher.java:65)
at org.hamcrest.Matcher$matches.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:128)
at io.restassured.assertion.BodyMatcher.validate(BodyMatcher.groovy:76)
at io.restassured.assertion.BodyMatcher$validate$0.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:144)
at io.restassured.assertion.BodyMatcherGroup$_validate_closure2.doCall(BodyMatcherGroup.groovy:47)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:264)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1034)
at groovy.lang.Closure.call(Closure.java:418)
at groovy.lang.Closure.call(Closure.java:434)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.collect(DefaultGroovyMethods.java:3287)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.collect(DefaultGroovyMethods.java:3257)
at org.codehaus.groovy.runtime.dgm$67.invoke(Unknown Source)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoMetaMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSite.java:274)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:56)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:128)
at io.restassured.assertion.BodyMatcherGroup.validate(BodyMatcherGroup.groovy:47)
at io.restassured.assertion.BodyMatcherGroup$validate$3.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:144)
at io.restassured.internal.ResponseSpecificationImpl$HamcrestAssertionClosure.validate(ResponseSpecificationImpl.groovy:481)
at io.restassured.internal.ResponseSpecificationImpl$HamcrestAssertionClosure$validate$1.call(Unknown Source)
at io.restassured.internal.ResponseSpecificationImpl.validateResponseIfRequired(ResponseSpecificationImpl.groovy:656)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:210)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:59)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:51)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:157)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:169)
at io.restassured.internal.ResponseSpecificationImpl.content(ResponseSpecificationImpl.groovy:97)
at io.restassured.specification.ResponseSpecification$content$1.callCurrent(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:51)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:157)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:177)
at io.restassured.internal.ResponseSpecificationImpl.body(ResponseSpecificationImpl.groovy:256)
at io.restassured.internal.ValidatableResponseOptionsImpl.body(ValidatableResponseOptionsImpl.java:268)
at Sample.Sample.AppTest.verifyJsonSchema(AppTest.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Caused by: com.fasterxml.jackson.core.JsonParseException: Unexpected character ('<' (code 60)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
at [Source: java.io.StringReader@56febdc; line: 2, column: 2]
at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1369)
at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:532)
at com.fasterxml.jackson.core.base.ParserMinimalBase._reportUnexpectedChar(ParserMinimalBase.java:453)
at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._handleUnexpectedValue(ReaderBasedJsonParser.java:1386)
at com.fasterxml.jackson.core.json.ReaderBasedJsonParser.nextToken(ReaderBasedJsonParser.java:669)
at com.fasterxml.jackson.databind.MappingIterator.hasNextValue(MappingIterator.java:159)
at com.github.fge.jackson.JsonNodeReader.readNode(JsonNodeReader.java:142)
at com.github.fge.jackson.JsonNodeReader.fromReader(JsonNodeReader.java:127)
at com.github.fge.jackson.JsonLoader.fromReader(JsonLoader.java:193)
at com.github.fge.jackson.JsonLoader.fromString(JsonLoader.java:206)
at io.restassured.module.jsv.JsonSchemaValidator.matchesSafely(JsonSchemaValidator.java:214)
... 80 more
另外,我想知道是否可以验证不同的数据类型,如字符、日期、数字?如果是,那么也请帮助我。
提前致谢...
解决方案
您的架构是错误的。您可以使用此架构。
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"books": {
"type": "array",
"items": [
{
"type": "object",
"properties": {
"isbn": {
"type": "string"
},
"title": {
"type": "string"
},
"subTitle": {
"type": "string"
},
"author": {
"type": "string"
},
"publish_date": {
"type": "string"
},
"publisher": {
"type": "string"
},
"pages": {
"type": "integer"
},
"description": {
"type": "string"
},
"website": {
"type": "string"
}
},
"required": [
"isbn",
"title",
"subTitle",
"author",
"publish_date",
"publisher",
"pages",
"description",
"website"
]
}
]
}
},
"required": [
"books"
]
}
json中有6种数据类型:数字、字符串、数组、对象、布尔值、空值。您无法验证字符、日期时间...
推荐阅读
- python - 字符串的正则表达式检测部分
- c++ - 如何在 QGraphicScene 上实时绘图?
- reactjs - CSS flex 子组件必须让子组件无法正确装入 3x2 flex 容器
- python-3.x - 使用 Django REST 框架获取与基本模型实例相关的所有模型的序列化响应
- python - 如何解决不支持 RELATIVE-OID 的 ASN.1 库
- tfs - VSTS 构建代理 web 代理配置
- java - 模板解析时出错(模板:“类路径资源 [templates/user-form/user-signup.html]”)
- visual-studio-2010 - C# 功能中的可选参数
- activemq-artemis - ActiveMQ Artemis 故障转移与集群
- omnet++ - 运行 Omnet 模拟时出现问题(退出代码 1)