问题
io.restassured.path.json.exception.JsonPathException: Failed to parse the JSON document
It is throwing error in the code written for these Feature file steps:
Given Add new location with "<name>" "<language>" "<address>" parameters
Then user calls "AddPlaceAPI" with http "POST" method
Then API successful response with "status" as "OK"
The stepDefinition code for these steps
@Given("Add new location with {string} {string} {string} parameters")
public void Add_new_location_with_three_parameters(String name_G,String address_G,String language_G) throws IOException {
res=given().spec(requestSpecification())
.body(data.googleAddPlace(name_G,address_G,language_G));
}
@Then("user calls {string} with http {string} method")
public void user_calls_with_http_method(String resource_G,String http_method)
{
APIResources resourceAPI = APIResources.valueOf(resource_G);
System.out.println("Resource value" + resourceAPI.getResource());
resspec =new ResponseSpecBuilder().expectStatusCode(200).expectContentType(ContentType.JSON).build();
if(http_method.equalsIgnoreCase("POST"))
response= res.when().post(resourceAPI.getResource());
else if(http_method.equalsIgnoreCase("GET"))
response= res.when().post(resourceAPI.getResource());
}
@Then("API successful response with {string} as {string}")
public void api_successful_response_with_as(String string, String string2) {
// Write code here that turns the phrase above into concrete actions
assertEquals(getJsonPath(response,string),string2);
}
getJsonPath
method Definition where it is throwing Error
public String getJsonPath(Response response,String key)
{
String resp=response.asString();
JsonPath js = new JsonPath(resp);
return js.get(key).toString();
}
enter image description here Junit Error Log
io.restassured.path.json.exception.JsonPathException: Failed to parse the JSON document at io.restassured.path.json.JsonPath$ExceptionCatcher.invoke(JsonPath.java:1002) at io.restassured.path.json.JsonPath$4.doParseWith(JsonPath.java:967) at io.restassured.path.json.JsonPath$JsonParser.parseWith(JsonPath.java:1047) at io.restassured.path.json.JsonPath.get(JsonPath.java:202) at resources.Utils.getJsonPath(Utils.java:56) at stepDefinations.stepDefination.api_successful_response_with_as(stepDefination.java:117) at ✽.API successful response with "status" as "OK"(file:///C:/Users/gaura/eclipse-workspaceNew/APIFramework/src/test/java/features/googlemapsValidation.feature:6) Caused by: groovy.json.JsonException: Lexing failed on line: 1, column: 1, while reading '<', no possible valid JSON value or punctuation could be recognized. at groovy.json.JsonLexer.nextToken(JsonLexer.java:86) at groovy.json.JsonLexer$nextToken.call(Unknown Source) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:130) at io.restassured.internal.path.json.ConfigurableJsonSlurper.parse(ConfigurableJsonSlurper.groovy:97) at io.restassured.internal.path.json.ConfigurableJsonSlurper$parse.callCurrent(Unknown Source) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:51) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:171) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:185) at io.restassured.internal.path.json.ConfigurableJsonSlurper.parseText(ConfigurableJsonSlurper.groovy:83) at io.restassured.path.json.JsonPath$4$1.method(JsonPath.java:965) at io.restassured.path.json.JsonPath$ExceptionCatcher.invoke(JsonPath.java:1000) at io.restassured.path.json.JsonPath$4.doParseWith(JsonPath.java:967) at io.restassured.path.json.JsonPath$JsonParser.parseWith(JsonPath.java:1047) at io.restassured.path.json.JsonPath.get(JsonPath.java:202) at resources.Utils.getJsonPath(Utils.java:56) at stepDefinations.stepDefination.api_successful_response_with_as(stepDefination.java:117) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.base/java.lang.reflect.Method.invoke(Unknown Source) at io.cucumber.java.Invoker.invoke(Invoker.java:27) at io.cucumber.java.JavaStepDefinition.execute(JavaStepDefinition.java:27) at io.cucumber.core.runner.PickleStepDefinitionMatch.runStep(PickleStepDefinitionMatch.java:63) at io.cucumber.core.runner.TestStep.executeStep(TestStep.java:64) at io.cucumber.core.runner.TestStep.run(TestStep.java:49) at io.cucumber.core.runner.PickleStepTestStep.run(PickleStepTestStep.java:46) at io.cucumber.core.runner.TestCase.run(TestCase.java:51) at io.cucumber.core.runner.Runner.runPickle(Runner.java:66) at io.cucumber.junit.PickleRunners$NoStepDescriptions.run(PickleRunners.java:149) at io.cucumber.junit.FeatureRunner.runChild(FeatureRunner.java:83) at io.cucumber.junit.FeatureRunner.runChild(FeatureRunner.java:24) at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329) at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293) at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306) at org.junit.runners.ParentRunner.run(ParentRunner.java:413) at io.cucumber.junit.Cucumber.runChild(Cucumber.java:185) at io.cucumber.junit.Cucumber.runChild(Cucumber.java:83) at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329) at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293) at io.cucumber.junit.Cucumber$RunCucumber.evaluate(Cucumber.java:219) at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306) at org.junit.runners.ParentRunner.run(ParentRunner.java:413) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
来源:https://stackoverflow.com/questions/62187173/i-am-unable-to-post-an-add-place-api-when-using-post-http-method