【问题标题】:How to setup time to wait for response in Rest-Assured?如何在 Rest-Assured 中设置等待响应的时间?
【发布时间】:2017-10-29 14:15:14
【问题描述】:

响应需要很长时间。怎么可能安心等待响应时间?

【问题讨论】:

    标签: response rest-assured time-wait


    【解决方案1】:

    过去我使用过等待,它允许您在启动另一个调用之前等待服务的响应。

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

    您可以返回提取的响应并等待状态代码/正文返回值。

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

    【讨论】:

      【解决方案2】:

      在这个类中你声明了最大时间

      public interface Constants {
      
      Long MAX_TIMEOUT = 3000l;
      

      }

      在这个类上你实现了接口

         public class BaseTest implements Constants {
      
            @BeforeClass
            public static void setup() {
      
            ResponseSpecBuilder resBuilder = new ResponseSpecBuilder();
            resBuilder.expectResponseTime(Matchers.lessThan(MAX_TIMEOUT));
            RestAssured.responseSpecification = resBuilder.build();
      }
      

      终于可以使用服务员策略了

      public class SimulationTest extends BaseTest {
      
      @Test
      public void checkStatus200() {
          given()
          .when()
              .get()
          .then()
              .statusCode(200)
          ;
      }
      }   
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多