![](/img/trans.png)
[英]Using queryParams instead of Json body in Restassured API testing
[英]RestAssured - using value of response from API A in the body of call to API B
所以我有一個 API (API A),我需要調用它來提供值。 我在下一個測試(API B)中需要這些值
public class testA {
String functionUrl = "https://dev.testA.v2";
@Test
public void RothHysa(ITestContext context) {
Map<String, Object> jsonRequest = new HashMap<String, Object>();
jsonRequest.put("product", "thisistheproduct");
Map<String,String> jurisdictionMap = new HashMap<>();
jurisdictionMap.put("category", "COUNTRY");
jurisdictionMap.put("name", "US");
jsonRequest.put("productCategories", productCategoriesMap);
System.out.println(jsonRequest);
Object RothHysa = RestAssured
.given()
.header("one-data-correlation-id", CommonUtility.getGuid())
.body(jsonRequest)
.when()
.post(functionUrl)
.then()
.extract().response()
.then().assertThat()
.statusCode(200)
.and()
.assertThat().body("idofproduct", equalTo(Arrays.asList("xxxxxxx-xxxx-xxx-xxxxxx-xxxxxx")))
.log().all()
.extract()
.jsonPath()
.get("idofproduct");
context.setAttribute("idofproduct", idToBeUsed);
System.out.println(idToBeUsed);
}
我斷言的值是一個數組 - API 將其作為 ['value'] 返回,這會在下一個測試中引起問題,因為它需要作為字符串傳遞。 我試過以各種方式將它轉換為 String ,但它們沒有奏效。 API B 測試:
public void Step1PCN(ITestContext context) {
String identifierIRArothHYSA1 = (String) context.getAttribute("identifier");
Map<String,Object> jsonRequest = new HashMap<>();
Map<String,String> value = new HashMap<>();
value.put("key","value");
value.put("key","value");
jsonRequest.put("key", value);
Map<String,String> value1 = new HashMap<>();
value1.put("key","value");
value1.put("key","value");
value1.put("key","value");
jsonRequest.put("key", value1);
jsonRequest.put("key", "value");
jsonRequest.put("productID", idToBeUsed);
Object idForTestB = RestAssured
.given()
.header("one-data-correlation-id", CommonUtility.getGuid())
.body(jsonRequest)
.when()
.post(functionUrl)
.then()
.contentType(ContentType.JSON)
.extract().response()
.then().assertThat()
.statusCode(200)
.and()
.assertThat().body("status.StatusMessage", equalTo("Success"))
.log().all()
.extract()
.jsonPath()
.get("idForTestB");
context.setAttribute("idForTestB", idForTestB);
}
然而 idToBeUsed 的值總是 [xxx-xxx-xxxxxx] 但在這個測試中它需要是一個字符串。
您可以將 var RothHysa
保存為List<String>
而不是Object
。
List<String> RothHysa = RestAssured.given()...get("idofproduct");
context.setAttribute("idForTestB", RothHysa.get(0));
String modifiedId = idForTestB.toString();
modifiedId = modifiedId.replaceAll("\\[", "");
modifiedId = modifiedId.replaceAll("]", "");
context.setAttribute("identifier[0]", modifiedId);
System.out.println(modifiedId);
這就是我最終要做的。 可能不是最有說服力的解決方案,但它有效
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.