Logging plays an important role in understanding the behaviour of the test. When we are testing an API, it is good to know how the APIs are behaving, how the request is made and how we received the response from API, what the headers looks like, what the body looks like, what parameters are we providing to the request. All this helps us in debugging the test code and to identify the reason for the failure of the test.
REST Assured, provide support to a different type of logging as shown below:-
- Request Logging
- Response Logging
- Logging to console
- Conditional logging
- Logging to a txt file on your machine
Request Logging
To log all request specification details including parameters, headers, and body of the request, log().all() needs to be added to post given() section.
@Test
public void requestLoggingDemo() {
String json = "{\"name\":\"apitest\",\"salary\":\"5000\",\"age\":\"30\"}";
// GIVEN
given()
.log().all()
.baseUri("http://dummy.restapiexample.com/api")
.contentType(ContentType.JSON)
.body(json)
// WHEN
.when()
.post("/v1/create")
// THEN
.then()
.assertThat()
.statusCode(200)
.body("data.name", equalTo("apitest"))
.body("message", equalTo("Successfully! Record has been added."));
}
The output of the above program is

Other different request logging options are:-
given().log().params(). .. // Log only the parameters of the request
given().log().body(). .. // Log only the request body
given().log().headers(). .. // Log only the request headers
given().log().cookies(). .. // Log only the request cookies
given().log().method(). .. // Log only the request method
given().log().path(). .. // Log only the request path
Response Logging
If you want to print the response body regardless of the status code, you can do
get("/x").then().log().body()..
This will print the response body regardless of an error occurring.
@Test
public void responseLoggingDemo() {
String json = "{\"name\":\"apitest\",\"salary\":\"5000\",\"age\":\"30\"}";
// GIVEN
given()
.baseUri("http://dummy.restapiexample.com/api")
.contentType(ContentType.JSON)
.body(json)
// WHEN
.when()
.post("/v1/create")
// THEN
.then()
.log().all()
.statusCode(200)
.body("data.name", equalTo("apitest"))
.body("message", equalTo("Successfully! Record has been added."));
}
The output of the above program is

Conditional Logging
What if you want to perform logging conditionally? For example, you want to log in only if validation fails or if the status code is equal to 200, or if the server returned a status code >=400.
.then().log().ifStatusCodeIsEqualTo(302). .. // Only log if the status code is equal to 302
.then().log().ifStatusCodeMatches(matcher). .. // Only log if the status code matches the supplied Hamcrest matcher
@Test
public void responseLoggingDemo() {
String json = "{\"name\":\"apitest\",\"salary\":\"5000\",\"age\":\"30\"}";
// GIVEN
given()
.baseUri("http://dummy.restapiexample.com/api")
.contentType(ContentType.JSON)
.body(json)
// WHEN
.when()
.post("/v1/create")
// THEN
.then()
.log().ifStatusCodeIsEqualTo(200)
.assertThat().statusCode(200)
.body("data.name", equalTo("apitest"))
.body("message", equalTo("Successfully! Record has been added."));
}
}
The output of the above program is

Logging to a text file with Rest Assured
We will see how we can log all the request and response data to a txt file using Rest Assured.
- Create a PrintStream object. You have to provide an object of FileInputStream() to the PrintStream() constructor. Provide the path to the logging.txt file in FileInputStream().
- REST Assured gives us filter() method, this filter method accepts RequestLoggingFilter and ResponseLoggingFilter. They have two methods, logRequestTo() and logResponseTo() methods respectively. These methods expect a Stream.
- Pass the log stream we created to these methods.
@Test
public void responseLoggingDemo() throws FileNotFoundException {
PrintStream log = new PrintStream(new FileOutputStream("logging.txt"));
String json = "{\"name\":\"apitest\",\"salary\":\"5000\",\"age\":\"30\"}";
// GIVEN
given()
.baseUri("http://dummy.restapiexample.com/api")
.contentType(ContentType.JSON)
.body(json)
.filter(RequestLoggingFilter.logRequestTo(log))
.filter(ResponseLoggingFilter.logResponseTo(log))
// WHEN
.when()
.post("/v1/create")
// THEN
.then()
.log().ifStatusCodeIsEqualTo(200)
.assertThat().statusCode(200)
.body("data.name", equalTo("apitest"))
.body("message", equalTo("Successfully! Record has been added."));
}
}

Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!
explained very detailed. thank you for sharing
LikeLike
I’m glad you liked it.
LikeLike