Restassured request with spec doesn't get response status logged

Viewed 54

I want my Restassured request with spec get response status logged. My request is

given()
                .spec(requestSpec)
                .when()
                .get("/" + userId)
                .then()              
                .spec(responseSpec)
                .extract().as(User.class)
                ;

My spec is

    public static ResponseSpecification responseSpec = new ResponseSpecBuilder()
            .expectStatusCode(200)
            .log(STATUS)
            .log(BODY)
            .expectBody(notNullValue())
            .build();
}

But there is no response status in the log unless I add .log().status() to my request.

Pls tell me how can I fix this.

UPDATE Following Ashish Patil advice I updated my request but without result

LogConfig logconfig = new LogConfig().enablePrettyPrinting(true);
RestAssured.config().logConfig(logconfig);


given()
        .spec(requestSpec)
        .when()
        .get("/" + userId)
        .then()              
        .spec(responseSpec)
        .extract().as(User.class)
        ;
2 Answers

Question didn't specify if you have set LogConfig before declaring your responseSpec. So you might need to declare LogConfig first to use log method as mentioned in doc :

LogConfig logconfig = new LogConfig().enablePrettyPrinting(true);
RestAssured.config().logConfig(logconfig);

And afterwords you can declare your responseSpec as per your question.

Actually, it's a bug. ResponseSpecification only supports one LogDetail at a time. For example:

...
.log(STATUS)
.log(BODY)
...

--> it will log BODY, not STATUS, because it is a variable declared private LogDetail responseLogDetail, the latter value will override the previous value.

  ResponseSpecificationImpl logDetail(LogDetail logDetail) {
    this.responseLogDetail = logDetail
    this
  }
Related