【问题标题】:Dependecy between two different json files in restassured放心的两个不同json文件之间的依赖关系
【发布时间】:2018-07-21 10:49:48
【问题描述】:

我使用 restAssured 创建了两个 java 类 TestA.java、TestB.java,其中每个类都从 TestA.json 读取 json和 testB.json 并向端点 uri.TestA.java 发布请求,返回带有标签“customerID”的 json 响应,该响应将作为 标签之一的输入>TestB.json 并且每当我使用“TestB.java”发布请求时,必须从 TestB.json 中挑选客户 ID。我的代码是什么样的?有什么想法吗?

我的代码:

TestA.java

String requestBody = generateString("CreateCustomer.json");
RestAssured.baseURI = "https://localhost:8080";
Response res = given().contentType(ContentType.JSON)
            .header("Content-Type", "application/json").header("checkXML", "N").body(requestBody).when()
            .post("/restservices/customerHierarchy/customers").then().assertThat()
            .statusCode(201).and().body("transactionDetails.statusMessage", equalTo("Success")).and().log().all()
            .extract().response();

    //converts response to string
    String respose = res.asString();
    JsonPath jsonRes = new JsonPath(respose);
    CustomerID = jsonRes.getString("customerNodeDetails.customerId");

TestA.java response
{
"customerNodeDetails": {

    "customerId": "81263"
},

现在我想将这个customerID 作为输入传递给testB.json 或testB.java,这是动态的。

TestB.json
 "hierarchyNodeDetails": { 
      "**customerId**":"", 
        "accountNumber": "", 
        "startDate": "", 
}

TestA.java 和 TestB.java 看起来几乎相同,除了 post uri

提前致谢

【问题讨论】:

    标签: json rest-assured rest-assured-jsonpath


    【解决方案1】:

    这取决于你如何分配你的课程:

    1. 如果您想在一个类中编写 A 和 B 的测试。声明一个 Response/String 类型的局部变量,然后将客户 ID 存储在该变量中。变量的范围将存在于所有 TestNG 方法中。您可以从本地变量中为 B.json 设置客户 ID。

      public class Test{  
      String _cust_id; 
      
      @Test
      public void test_A(){
          // ceremony for getting customer id
          _cust_id = jsonRes.getString("customerNodeDetails.customerId");
      }
      
      @Test
      public void test_B(){
          // write value of customer id using the variable _cust_id
      }}
      

    您可以尝试这种方法,但建议将数据部分分离到 dataProvider 类中。

    • 如果您想为 A 和 B 设置单独的类,请使用 ITestContext 将值从一个类传递到另一个类。

          public class A{
              @Test
              public void test1(ITestContext context){
                  context.setAttribute("key", "value");
              }
          }
      
          public class B{
              @Test
              public void test2(ITestContext context){
                  String _attribute = context.getAttribute(key);
              }
          }
      
    • 优雅的方式可能是,将 dataProvider 用于 B 类测试,您可以在其中执行从 A 类测试中获取 customerID 的仪式。

      public class DataB{
      @DataProvider
      public static Object[][] _test_data_for_b(){
          // get the customerID
          // create the json for class B
          // return the json required for class B test
          // All these could be achieved as everything has a common scope
      }}
      
      public class B{
      @Test(dataProvider = "_test_data_for_b", dataProviderClass = DataB.class)
      public void test_B(String _obj){
          // perform the testing
      }}
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-14
      • 2021-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-01
      • 2019-12-03
      • 1970-01-01
      相关资源
      最近更新 更多