简体   繁体   English

RestAssured - 在调用 API B 的主体中使用来自 API A 的响应值

[英]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.

相关问题 在放心的 API 测试中使用 queryParams 而不是 Json 主体 - Using queryParams instead of Json body in Restassured API testing 我想从一个 API 响应正文中获取价值,并使用 Cucumber gherkin 将其用于另一个 api 请求 - I want to take value from one API response body and use into another api request using Cucumber gherkin 更新来自 API 的 JSON 响应,并使用 Rest Assured 将更新后的响应作为输入/正文传递给另一个 API - Update JSON response from an API and pass the updated response as input/body to another API using Rest Assured 如何在 JAVA 的再保证 api 的请求正文中传递参数? - How to pass parameter in request body of restassured api in JAVA? 如何解析和获取一些,验证来自再保证 api 和 Java 的响应中的值 - How to parse and get some,verify values from response in restassured api with Java 带有边界的放心的解析身体响应 - Restassured Parse Body response with boundary 使用Regex断言RestAssured响应体 - Assert RestAssured response body with Regex API测试-在对JSON使用restassured时导致方括号中的值导致断言失败 - API Testing - getting value in squarebraces when using restassured for json resulting in assertion failure RestAssured - 想在 RestAssured 中验证 JSON 响应的主体结构 - RestAssured - want to verify the body structure of JSON response in RestAssured 如何在放心的回应中获取价值 - How to retrieve value in the response of restassured
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM