RestAssured returns 数组而不是字符串
RestAssured returns array instead of String
我正在用 RestAssured 编写这个测试。我想提取 String 中的 id 但始终将其作为数组返回。这是我到目前为止写的测试。
@Test(priority = 1)
public static void searchForUsername( ) throws Throwable {
ValidatableResponse response= given().contentType(ContentType.JSON).queryParam("username",
"Delphine").log().parameters().get("https://jsonplaceholder.typicode.com/users").then().log().all();
JsonPath extractor = response.extract().jsonPath();
userId = extractor.getString("id");
System.out.println(userId);
}
它打印为 [9] 而不是 9
原因:您提取的对象在数组中。所以当你提取时,id
将在一个数组中。
解决方案:从id
的列表中,可以通过索引得到。
@Test
void name2() {
Response res = RestAssured.given()
.queryParam("username", "Delphine")
.get("https://jsonplaceholder.typicode.com/users");
int id = (Integer) res.jsonPath().getList("id").get(0);
System.out.println(id);
}
我正在用 RestAssured 编写这个测试。我想提取 String 中的 id 但始终将其作为数组返回。这是我到目前为止写的测试。
@Test(priority = 1)
public static void searchForUsername( ) throws Throwable {
ValidatableResponse response= given().contentType(ContentType.JSON).queryParam("username",
"Delphine").log().parameters().get("https://jsonplaceholder.typicode.com/users").then().log().all();
JsonPath extractor = response.extract().jsonPath();
userId = extractor.getString("id");
System.out.println(userId);
}
它打印为 [9] 而不是 9
原因:您提取的对象在数组中。所以当你提取时,id
将在一个数组中。
解决方案:从id
的列表中,可以通过索引得到。
@Test
void name2() {
Response res = RestAssured.given()
.queryParam("username", "Delphine")
.get("https://jsonplaceholder.typicode.com/users");
int id = (Integer) res.jsonPath().getList("id").get(0);
System.out.println(id);
}