Skip to main content

REST Assured Log if validation fails

Since REST Assured 2.3.1 you can log the request or response only if the validation fails.

Log Request

// To log all Request information
given().log().ifValidationFails()

// To log specific request information, like in below to log Request Method
given().log().ifValidationFails(LogDetail.METHOD)

Log Response

// Log all the response information
.log().ifValidationFails()

// Log specific response information, like body in below example
.log().ifValidationFails(LogDetail.BODY)

// Log specific response information, like body in below example
// Set true to pretty-print 
.log().ifValidationFails(LogDetail.BODY, true)

Log Request and Response Together

// Log all detail
given().config(RestAssured.config().logConfig(LogConfig.logConfig().enableLoggingOfRequestAndResponseIfValidationFails()))

// Log only header or other specific detail
given().config(RestAssured.config().logConfig(LogConfig.logConfig().enableLoggingOfRequestAndResponseIfValidationFails(LogDetail.HEADERS)))

Log Request and Response Together - Shortcut

Use it outside given()

// To log everything
RestAssured.enableLoggingOfRequestAndResponseIfValidationFails();

// To log request and response header
RestAssured.enableLoggingOfRequestAndResponseIfValidationFails(LogDetail.HEADERS);

Submitted by tgoswami on September 2, 2020

Tarun has 11+ years of experience in Quality Assurance in different domains like Banking, E-commerce, Health and Education.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.