我目前有一些需要重试逻辑的代码。由于某些远程请求和数据库更新可能需要一些时间。我不想为此添加特定的等待。我想重试,直到它匹配我的特定答复。当前,它正在使用等待性,它将重试,直到匹配特定字段为止。
private static RequestSpecification REQUEST_SPEC(String baseUri) {
return new RequestSpecBuilder()
.setBaseUri(baseUri)
.setBasePath(DEFAULT_PATH)
.setAccept(ContentType.JSON)
.setContentType(ContentType.JSON)
.setConfig(config().logConfig(logConfig().enableLoggingOfRequestAndResponseIfValidationFails()))
.build();
}
private static ResponseSpecBuilder PROFILE_RESPONSE() {
return new ResponseSpecBuilder()
.expectStatusCode(HTTP_OK)
.expectBody("profileId", equalTo(PROFILE_ID))
.expectBody("userKey", notNullValue());
}
await().timeout(10, SECONDS).untilAsserted(() -> assertThat("Profile not yet updated",
given()
.spec(REQUEST_SPEC)
.pathParam("somePathParam", "somePathParamValue")
.auth().oauth2("someAccessToken")
.when()
.get("/someRequestPath")
.then()
.spec(PROFILE_RESPONSE)
.extract()
.body().jsonPath().getString("profileId"), equalTo("updatedProfileId")));
我正在将内置的RestAssured机制(可重复使用的ResponseSpecs规范)和等待时间与Hamcrest Matcher结合在一起。有没有一种方法可以重试RestAssured ValidatableResponse而不是提取它并使用hamcrest assertThat()匹配器对其进行验证?
答案 0 :(得分:0)
Awaitility.await().atMost(30,SECONDS).until(()-> isStatus(expected));
private Boolean isStatus(String expected){
String url="/order/{id}";
Response response = RestAssured.given()
.header("header", "value")
.pathParam("id",id)
.when().get(url);
return response.then().extract().path("links").toString().equalsIgnoreCase(expected)
}