简体   繁体   中英

How to setup time to wait for response in Rest-Assured?

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

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();
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM