3

I have created an api test which hits an endpoint and receives a response back however I'm struggling to use this response within another cucumber step.

My first step uses the following method:

public Response booking(SharedStepData sharedStepData, String path, BookingType bookingType) throws IOException {
    String url = "https://example.net." + System.getProperty("endpoint") + "/v10/" + path + "Booking";

    RestAssured.useRelaxedHTTPSValidation();
    String payload = createBookingPayload(sharedStepData, bookingType);
    Response response = RestAssured
            .given().contentType(ContentType.JSON)
            .log().all()
            .body(payload)
            .when().post(url)
            .thenReturn();

    ResponseBody body = response.getBody();;
    return response;
}

I know need to save this response and then use it within another step method to perform another action, such as using specific data from the response to hit another endpoint, any ideas?

1
  • Store it in a private field declared for the stepdef class. Commented Jun 27, 2018 at 15:52

2 Answers 2

1

You can use the encapsulation logic here and can access the variable from other class

public class Test{

    private String bookingResponse;

    public String getBookingResponse(){
        return bookingResponse;
    }

    public void setBookingResponse(String response){
        bookingResponse=response;
    } 
}

After Calling the first method, you can update the bookingResponse value as below

Test test=new Test();

test.setBookingResponse(<<Call your first Response Method>>);

Whenever you want to access the above response, then you can use the test.getResponse() method. It will give the above response, if you are using the same Test class instance.

Sign up to request clarification or add additional context in comments.

Comments

0

The way to share state between steps is to store it in a field in the class where the steps are implemented.

The way to share state between steps when the steps are implemented in different classes is to share a common object in both implementation classes. The idiomatic way to do this in Java is to use dependency injection.

I have written a couple of blog posts on this topic, they may be of use for you. The easiest solution is to use PicoContainer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.