0

I have Created two java classes TestA.java,TestB.java using restAssured where each of the class reads json from TestA.json and testB.json and post a request to endpoint uri.TestA.java returns a json response having tag "customerID" which will be input for one of the tags of TestB.json and when ever I post a request using "TestB.java" customerID has to be picked from TestB.json .How do my code look like?Any ideas?

My code :

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"
},

Now i want to pass this customerID as input in testB.json or testB.java which is dynamic.

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

Both TestA.java and TestB.java looks almost same except the post uri

Thanks in Advance

1 Answers1

0

It depends on how you are distributing your classes:

  1. If you want to write the tests for A and B in a single class. Declare a local variable of type Response/String and then store the customer ID in that variable. The scope of the variable will be live in all TestNG methods. You can set the customer ID for the B.json from the local variable.

    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
    }}
    

You can try this approach, but would suggest separating the data part to a dataProvider class.

  • If you want to have separate classes for A and B, use ITestContext to pass values from one class to the other.

        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);
            }
        }
    
  • The elegant way could be, use a dataProvider for class B test where you perform the ceremony of getting the customerID from class A Tests.

    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
    }}