我正在尝试使用Rest Assured 3.10验证Places to Search google API的JSON架构,并收到io.restassered.module.jsv.JsonSchemaValidationException:com.fasterxml.jackson.core.JsonParseException:意外字符(’s'(代码115((:应为有效值(数字、字符串、数组、对象、'true'、'false'或'null'(错误。
我已经使用在线JSON模式验证器验证了该模式,似乎没有问题。
以下是代码:
public void validateJSONSchema() {
RestAssured.baseURI="https://maps.googleapis.com/";
given().
queryParam("location","-33.8670522,151.1957362").
queryParam("radius","1500").
queryParam("type","restaurant").
queryParam("keyword","cruise").
queryParam("key",<API Key>).
when().
get("maps/api/place/nearbysearch/json").
then().assertThat().statusCode(200).contentType(ContentType.JSON).
body(matchesJsonSchema("src\main\resources\googleAPIs\json\placesearch.json"));
}
我收到以下错误:
FAILED: validateJSONSchema
io.restassured.module.jsv.JsonSchemaValidationException: com.fasterxml.jackson.core.JsonParseException: Unexpected character ('s' (code 115)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
at [Source: java.io.StringReader@21526f6c; line: 1, column: 2]
at io.restassured.module.jsv.JsonSchemaValidator$JsonSchemaValidatorFactory.create(JsonSchemaValidator.java:286)
at io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchema(JsonSchemaValidator.java:107)
at googleapis.FindaPlaceTest.validateJSONSchema(FindaPlaceTest.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1198)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1123)
at org.testng.TestNG.run(TestNG.java:1031)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
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 ('s' (code 115)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
at [Source: java.io.StringReader@21526f6c; line: 1, 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:179)
at com.github.fge.jackson.JsonLoader.fromString(JsonLoader.java:192)
at io.restassured.module.jsv.JsonSchemaValidator$1.createSchemaInstance(JsonSchemaValidator.java:105)
at io.restassured.module.jsv.JsonSchemaValidator$1.createSchemaInstance(JsonSchemaValidator.java:102)
at io.restassured.module.jsv.JsonSchemaValidator$JsonSchemaValidatorFactory.create(JsonSchemaValidator.java:284)
... 26 more
JSON架构
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"candidates": {
"type": "array",
"items": [
{
"type": "object",
"properties": {
"formatted_address": {
"type": "string"
},
"geometry": {
"type": "object",
"properties": {
"location": {
"type": "object",
"properties": {
"lat": {
"type": "number"
},
"lng": {
"type": "number"
}
},
"required": [
"lat",
"lng"
]
},
"viewport": {
"type": "object",
"properties": {
"northeast": {
"type": "object",
"properties": {
"lat": {
"type": "number"
},
"lng": {
"type": "number"
}
},
"required": [
"lat",
"lng"
]
},
"southwest": {
"type": "object",
"properties": {
"lat": {
"type": "number"
},
"lng": {
"type": "number"
}
},
"required": [
"lat",
"lng"
]
}
},
"required": [
"northeast",
"southwest"
]
}
},
"required": [
"location",
"viewport"
]
},
"name": {
"type": "string"
},
"opening_hours": {
"type": "object",
"properties": {
"open_now": {
"type": "boolean"
},
"weekday_text": {
"type": "array",
"items": {
}
}
},
"required": [
"open_now",
"weekday_text"
]
},
"photos": {
"type": "array",
"items": [
{
"type": "object",
"properties": {
"height": {
"type": "integer"
},
"html_attributions": {
"type": "array",
"items": [
{
"type": "string"
}
]
},
"photo_reference": {
"type": "string"
},
"width": {
"type": "integer"
}
},
"required": [
"height",
"html_attributions",
"photo_reference",
"width"
]
}
]
},
"rating": {
"type": "number"
}
},
"required": [
"formatted_address",
"geometry",
"name",
"opening_hours",
"photos",
"rating"
]
}
]
},
"debug_log": {
"type": "object",
"properties": {
"line": {
"type": "array",
"items": {
}
}
},
"required": [
"line"
]
},
"status": {
"type": "string"
}
},
"required": [
"candidates",
"debug_log",
"status"
]
}
尝试使用此代码
String expectedJson = FileUtils.readFileToString(new File("src/test/resources/uploads/mobilesdk.json"));
response.then().assertThat().body(matchesJsonSchema(expectedJson));
尝试读取json-to-string,然后断言它。