[英]RestAssured - using value of response from API A in the body of call to API B
so i have an API (API A) that i need to call that provides values.所以我有一个 API (API A),我需要调用它来提供值。 I need those values in the next test (API B)
我在下一个测试(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);
}
the value i assert IS an array - the API returns it as ['value'] which is causing problems in the next test because it needs to be passed as a string.我断言的值是一个数组 - API 将其作为 ['value'] 返回,这会在下一个测试中引起问题,因为它需要作为字符串传递。 I've tried converting it to String various ways but they haven't worked.
我试过以各种方式将它转换为 String ,但它们没有奏效。 API B test:
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);
} }
however the value of idToBeUsed is always [xxx-xxx-xxxxxx] but in this test it needs to be a string.然而 idToBeUsed 的值总是 [xxx-xxx-xxxxxx] 但在这个测试中它需要是一个字符串。
You can save var RothHysa
as List<String>
instead of Object
.您可以将 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);
this is what i ended up doing.这就是我最终要做的。 probably not the most eloquent solution but it works
可能不是最有说服力的解决方案,但它有效
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.