Home > Blockchain >  Can Rest-Assured's response payload be stored and used across multiple classes?
Can Rest-Assured's response payload be stored and used across multiple classes?

Time:03-13

With Rest-Assured, I send a GET request and get the correct response as requested. I am able to then assert that the correct HTTP status code is returned.

public class MyClass{

  private static Response response;
  RestUtil restUtil = new RestUtil();


  public void sendGetRequestT{
    RequestSpecification httpRequest = given()
            .header("Content-Type", "application/json");

    response = httpRequest
            .when()
            .get(restUtil.getUrl()).andReturn();
  }


 
  public void verifyStatusCode() {
    int statusCode = response.getStatusCode();
    assertThat(response.getStatusCode()).isEqualTo(200);
  }}

Because I would need to verify the correct status code every time with new requests, I thought it was a smart idea to move the verifyStatusCode() to a util class so that I can call it whenever needed, so I put the implementation that verifies status code in a separate class:

public class RestUtil {
  public static Response response;

  public String url = https://gorest.co.in/public/v1/users;


  public void verify200() {
    int statusCode = response.getStatusCode();
    System.out.println("Status code: "   statusCode);
    assertThat(response.getStatusCode()).isEqualTo(200);

  }

}

Now, in the test class, I called the verify200() method but got a NullPointerException:

public class MyClass{
    
      private static Response response;
      RestUtil restUtil = new RestUtil();
    
    
      
      public void sendGetRequestT{
        RequestSpecification httpRequest = given()
                .header("Content-Type", "application/json");
    
        response = httpRequest
                .when()
                .get(restUtil.getUrl()).andReturn();
      }
    
    
     
      public void verifyStatusCode() {
        restUtil.verify200(); // NullPointerException thrown here
      }}

It looks like using the response across multiple classes is not possible, or perhaps I am doing something wrong. What could be wrong?

CodePudding user response:

Having public static variables is not a good idea. In addition, the Response variable in RestUtil has no relationship to the one in MyClass, so it is null. A better approach is to pass the Response into your util method such as:

public class RestUtil {

  public static void verify200(Response response) {
    int statusCode = response.getStatusCode();
    System.out.println("Status code: "   statusCode);
    assertThat(response.getStatusCode()).isEqualTo(200);

  }

}

Then in MyClass:

public class MyClass{
    
      private Response response;
     
      public void sendGetRequestT() {
        RequestSpecification httpRequest = given()
                .header("Content-Type", "application/json");
    
        response = httpRequest
                .when()
                .get(restUtil.getUrl()).andReturn();
      }     
     
      public void verifyStatusCode() {
        RestUtil.verify200( response); 
      }}
  
  • Related