Logging in Rest Assured

In this chapter, we are going to learn how to log with rest assured. We will be learning how the log will be used in different places. If you do not have the log messages, then it becomes difficult to debug the software.

In rest assured, we can log in multiple places, but two important places are after preparing the request and after receiving the response from the server.

We can log different values used in the rest assured; all the log fields are used after the log() method.

For example, if you want to log the headers that we are passing into a server, then we can use headers() method to log the headers.

All the logging methods are present in LogSpecification class

Example of logging at all the items

import org.junit.jupiter.api.Test;
import io.restassured.RestAssured;
public class FirstTest{
	@Test
	void testLogging() {
		RestAssured.given()
		.when()
		.log()
		.all()
		.get("https://chercher.tech/sample/api/product/read");	
	}
}

logging-rest-assured

In the above example, we have logged all the information using the all() method, and we have a few more logging methods present in Rest Assured.

Different methods in Logs

We can log all the information before we are sending the request to the server; few methods are useful for logging.

Few Methods have an overloaded method to print the output prettier or uglier.

These methods accept a parameter called shouldPrettyPrint , and used to print the output with indentation and pretty.

Set the shouldPrettyPrint true if the body should be pretty-printed, false if you want uglier output. By default, the value of the shouldPrettyPrint considered as true.

Note that pretty-printing can only take place if the content-type is either XML, JSON, or HTML.

all() :

Logs everything in the specification, including, e.g., headers, cookies, body with the option to pretty-print the body if the content-type is either XML, JSON, or HTML. overloaded method is all(boolean shouldPrettyPrint)

@Test
void testLogging() {
	RestAssured.given()
	.when()
	.log().all() // after preparing request but not yet send to server
	.get("https://chercher.tech/sample/api/product/read")
	.then()
	.log().all(); // after receiving the response from server
}

logging-rest-assured-before-after

body():

Logs only the content of the body. The body will be pretty-printed by default if content-type is either XML, JSON, or HTML. The overloaded method body(boolean shouldPrettyPrint)

@Test
void testLogging() {
	RestAssured.given()
	.body("{
" + 
			"	"name": "Latest gadget",
" + 
			"	"description": "New version is released",
" + 
			"	"price": "10"
" + 
			"}")
	.when()
	.log().body() // after preparing request but not yet send to server
	.put("https://chercher.tech/sample/api/product/read")
	.then()
	.log().body(); // after receiving the response from server
}

body-log-rest-assured

everything():

This method is equivalent to all() method; everything() method will print all the details

status():

Logs only the status line (includes the status code).

@Test
void testLogging() {
	RestAssured.given()		
	.when()
	.get("https://chercher.tech/jsdbvbkv") // url supplied doenot exist, so result in error
	.then()
	.log().status();
}
// output
HTTP/1.1 404 Not Found

headers():

Logs only the headers. No overloaded method is present.

@Test
void testLogging() {
	RestAssured.given()
	.contentType(ContentType.JSON)
	.body("{
" + 
			"	"name": "Latest gadget",
" + 
			"	"description": "New version is released",
" + 
			"	"price": "10"
" + 
			"}")
	.when()
	.log().headers() // after preparing request but not yet send to server
	.put("https://chercher.tech/sample/api/product/create")
	.then()
	.log().headers(); // after receiving the response from server
}

before-after-log-rest-assured-headers

cookies():

Logs only the cookies. No overloaded method is present.

@Test
void testLogging() {
	RestAssured.given()
	.contentType(ContentType.JSON)
	.body("{
" + 
			"	"name": "Latest gadget",
" + 
			"	"description": "New version is released",
" + 
			"	"price": "10"
" + 
			"}")
	.when()
	.log().cookies() // after preparing request but not yet send to server
	.put("https://chercher.tech/sample/api/product/create")
	.then()
	.log().cookies(); // after receiving the response from server
}

cookies-rest-assured-logging

ifValidationFails():

So for whatever we have seen, those logs will be printed irrespective of your test validations. But ifValidationFails() method will print only in case if the given validation is failed; otherwise, it will not print anything.

ifValidationFails() print every detail, applicable only for responses.

@Test
void testLogging() {
	RestAssured.given()
	.contentType(ContentType.JSON)
	.body("{
" + 
			"	"name": "Latest gadget",
" + 
			"	"description": "New version is released",
" + 
			"	"price": "10"
" + 
			"}")
	.when()
	.put("https://chercher.tech/sample/api/product/create")
	.then()
	.log().ifValidationFails()
	.statusCode(202); // we will receive 201 but for failing the test i have given 202
}

if-validation-fails-rest-assured

ifValidationFails(LogDetail logDetail):

We can set the log level we wanted in case the validation fails.

LogDetail enum in Rest Assured

LogDetail enum has different levels of logging, and Logs will be printed based on the value we specify.

  • ALL : Logs everything
  • HEADERS : Log only headers
  • COOKIES : Log only cookies
  • BODY : Logs the content of the body
  • STATUS : Log only the status line (works only for responses).
  • PARAMS : Logs only the request parameters (only works for requests)
  • METHOD : Logs only the request method (only works for requests)
  • URI : Logs only the request uri (only works for requests)
@Test
void testLogging() {
	RestAssured.given()
	.contentType(ContentType.JSON)
	.body("{
" + 
			"	"name": "Latest gadget",
" + 
			"	"description": "New version is released",
" + 
			"	"price": "10"
" + 
			"}")
	.when()
	.put("https://chercher.tech/sample/api/product/create")
	.then()
	.log().ifValidationFails(LogDetail.STATUS)
	.statusCode(202); // we will receive 201 but for failing the test i have given 202
}

status-log-details-rest-assured-logging

ifError():

Logs everything only if an error occurs (status code >= 400).

@Test
void testLogging() {
	RestAssured.given()		
	.when()
	.get("https://chercher.tech/jsdbvbkv") // url supplied doenot exist, so result in error
	.then()
	.log().ifError();
}

if-error-logging-rest-assured

ifStatusCodeIsEqualTo

Logs everything only if the status code is equal to the given statusCode.

Video

About Author :

I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions