Reputation: 379
I want to add a condition to Rest Assured. For example, if I have input 1, the request should be checked for condition 1, but if I do not have input 1, condition 1 should not be checked. Of course I could do the if outside the request and just not add the check, but I have several cases like this this is a lot of unnecessary code. Is there any way I can save code using an inline if?
if (condition1){
response =
given().
spec(spec).
body(data).
when().
post("/test").
then().
assertThat().
statusCode(201).
body("id", 1).
extract().
response();
} else {
response =
given().
spec(spec).
body(data).
when().
post("/test").
then().
assertThat().
statusCode(201).
//DONT DO THE CHECK
extract().
response();
}
Is there a way to do that in one line? Something like this:
response =
given().
spec(spec).
body(data).
when().
post("/test").
then().
assertThat().
statusCode(201).
if condition do this body("id", 1) otherwise dont do anything
extract().
response();
Upvotes: 1
Views: 1620
Reputation: 11
You can try something like this:
ValidatableResponse validatableResponse =
given()
.spec(spec)
.body(data)
.when()
.post(endpoint)
.then()
.spec(responseSpec)
.assertThat();
if (condition) {
validatableResponse.body("id", 1)
}
Response response = validatableResponse.extract().response();
Upvotes: 1