I want to verify that id: 1 belongs to Tiger Nixon
{"status":"success","data":[{"id":"1","employee_name":"Tiger Nixon","employee_salary":"320800","employee_age":"61","profile_image":""}
I have been following this document: https://github.com/rest-assured/rest-assured/wiki/Usage#json-using-jsonpath
, this section body("shopping.category.find { it.@type == 'groceries' }.item", hasItems("Chocolate", "Coffee"));
Currently I am getting a null pointer exception. I could use some help to come up with a solution. Thanks in advance for your time.
import com.google.gson.JsonObject;
import io.restassured.RestAssured;
import org.testng.Assert;
public class RestAssuredExample_2 {
public void getResponse() {
JsonObject responseObject = RestAssured.get("http://dummy.restapiexample.com/api/v1/employees")
.then()
.extract().response().as(JsonObject.class);
String emp1Name = responseObject.get("data.find{it.@id=='1'}.employee_name").toString();
Assert.assertEquals(emp1Name, "Tiger Nixon");
}
public static void main(String[] args) {
RestAssuredExample_2 rest2 = new RestAssuredExample_2();
rest2.getResponse();
}
}
Error:
Exception in thread "main" java.lang.NullPointerException
at HTTPz.RestAssuredExample_2.getResponse(RestAssuredExample_2.java:16)
Tried to replicate this scenario
String responseObject = RestAssured.get("http://dummy.restapiexample.com/api/v1/employees").then().extract().asString();
JsonPath js = new JsonPath(responseObject);
String emp1Name = js.get("data.find {it.id =='1'}.employee_name").toString();
System.out.println(emp1Name);
And I get the value for emp1Name as " Tiger Nixon "
Difference:
Original: it.@id=='1'
Mine: it.id =='1'
It seems like @ is for XMLPath and not for JSONPath and honestly I wouldn't know about it in detail as well cause I have been using JSONPath only for a very long time:)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.