![](/img/trans.png)
[英]API Testing - getting value in squarebraces when using restassured for json resulting in assertion failure
[英]Using queryParams instead of Json body in Restassured API testing
我正在使用 RestAssured 庫以 Java 語言自動化 API 響應,這是我使用的 API 正文:
{
"meta": {
"language": "en",
"marketCountry": "IN",
"sourceUrl": "https://cj-gaq-dev.logistics.dhl/regular-shipment.html?en-AU"
},
"contactInformation": {
"company": "test",
"firstName": "test",
"lastName": "test",
"address": "test",
"zip": "test",
"city": "test",
"country": "IN",
"countryDisplay": "India",
"email": "test@test.com",
"phoneNumber": "2324243243",
"comments": "test"
},
"shipmentScale": {
"domestic": false,
"regional": false,
"global": true
},
"shipmentProduct": {
"FREIGHT": {
"numberOfShipments": "50",
"frequency": "WEEKLY",
"checked": true
}
}
而不是使用整個 api 主體,我想使用 queryParameters。 有沒有辦法做到這一點?
這是我到目前為止一直在使用的,並且不斷收到 422 狀態代碼錯誤:
String result = given().header("Content-Type","application/json" )
.header("Accept","application/json").log().all().queryParam("marketCountry", "IN").queryParam("shipmentScale.domestic", "false")
.queryParam("shipmentScale.regional", "false").queryParam("shipmentScale.global", "true")
.queryParam("shipmentProduct.FREIGHT.checked", "true")
.queryParam("shipmentProduct.FREIGHT.numberOfShipments", "50")
.queryParam("shipmentProduct.FREIGHT.frequency", "WEEKLY")
.queryParam("contactInformation.company", "test")
.queryParam("contactInformation.firstName", "test")
.queryParam("contactInformation.lastName", "test")
.queryParam("contactInformation.address", "test")
.queryParam("contactInformation.zip", "test")
.queryParam("contactInformation.city", "test")
.queryParam("contactInformation.email", "test@test.com")
.queryParam("contactInformation.phoneNumber", "213456")
.queryParam("contactInformation.comments", "test")
.queryParam("contactInformation.country", "IN")
.queryParam("contactInformation.comments", "test")
.when().post().then().assertThat().statusCode(200).extract().response().asString();
一種簡單的方法是使用 RestAssured 庫以 Java 語言自動執行 API 響應。 請參閱下面的 java class:
public class Basics {
public static void main(String[] args) {
RestAssured.baseURI = "https://12.23.454.55";
given().log().all().queryParam("key", "mykey123").header("Content-Type","application/json")
.body("{\r\n" +
"\r\n" +
"\r\n" +
"\r\n" +
"\"meta\": {\r\n" +
" \"language\": \"en\",\r\n" +
" \"marketCountry\": \"IN\",\r\n" +
" \"sourceUrl\": \"https://cj-gaq-dev.logistics.dhl/regular-shipment.html?en-AU\"\r\n" +
"},\r\n" +
"\"contactInformation\": {\r\n" +
" \"company\": \"test\",\r\n" +
" \"firstName\": \"test\",\r\n" +
" \"lastName\": \"test\",\r\n" +
" \"address\": \"test\",\r\n" +
" \"zip\": \"test\",\r\n" +
" \"city\": \"test\",\r\n" +
" \"country\": \"IN\",\r\n" +
" \"countryDisplay\": \"India\",\r\n" +
" \"email\": \"test@test.com\",\r\n" +
" \"phoneNumber\": \"2324243243\",\r\n" +
" \"comments\": \"test\"\r\n" +
"},\r\n" +
"\"shipmentScale\": {\r\n" +
" \"domestic\": false,\r\n" +
" \"regional\": false,\r\n" +
" \"global\": true\r\n" +
"},\r\n" +
"\"shipmentProduct\": {\r\n" +
" \"FREIGHT\": {\r\n" +
" \"numberOfShipments\": \"50\",\r\n" +
" \"frequency\": \"WEEKLY\",\r\n" +
" \"checked\": true\r\n" +
" }\r\n" +
"\r\n" +
"\r\n" +
"\r\n" +
"}").when().post("api/add/json")
.then().log().all().assertThat().statusCode(200);
}
}
如果您願意,您可以對響應進行更多驗證,或者您可以使用 JsonPath class 來解析您的 json 響應以進行進一步驗證。
但是,另一種方法是將 json 請求保留在單獨的類方法中,並在請求正文中返回 object。 或者更好的是,您可以探索 POJO java class 的可能性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.