2

Response takes a long time to come. How it is possible to wait for response time in rest-assured ?

IrinaG
  • 23
  • 1
  • 1
  • 4

2 Answers2

8

In the past I've used awaitility, it allows you to wait for a response from the service before kicking off another call.

https://github.com/awaitility/awaitility.

You can return an extracted response and wait for the status code/body to return a value.

@Test
public void waitTest() throws Exception {
    Awaitility.await().atMost(5, TimeUnit.SECONDS).until(() -> this.getStatus() == 200)
}

public int getStatus() {
    return given()
        .accept(ContentType.JSON)
        .get(url)
        .then()
        .extract()
        .statusCode();
}
Ciaran George
  • 571
  • 5
  • 19
1

On this class you declared the max time

public interface Constants {

Long MAX_TIMEOUT = 3000l;

}

Here on this class you implement the interface

   public class BaseTest implements Constants {

      @BeforeClass
      public static void setup() {

      ResponseSpecBuilder resBuilder = new ResponseSpecBuilder();
      resBuilder.expectResponseTime(Matchers.lessThan(MAX_TIMEOUT));
      RestAssured.responseSpecification = resBuilder.build();
}

Finally you can use the waiter strategy

public class SimulationTest extends BaseTest {

@Test
public void checkStatus200() {
    given()
    .when()
        .get()
    .then()
        .statusCode(200)
    ;
}
}   
anderson mann
  • 11
  • 1
  • 4