Debugging RestAssured Tests

When you run REST Assured tests, you may encounter errors such as failing assertions, incorrect responses, or unexpected behaviors. This article explains some techniques that can help you identify the root cause of these errors and fix them quickly. By debugging your tests, you can see what’s happening under the hood and find the issues that are causing the test failures.

The Basics of RESTAssured

REST Assured is a Java library that provides a domain-specific language (DSL) for testing RESTful web services. It allows you to write tests that send HTTP requests and validate the responses received from the server.

To learn more about RESTAssured, we recommend checking this article: How to test REST Services with RestAssured

There are several techniques that you can apply to troubleshoot failing tests. The most obvious is that you can place breakpoints in your Tests.

restassured debug print body

Sometimes, however, you are not able to attach a breakpoint to the Test or simply you want to collect some extra information from your code. Let’s see how to improve the logs of your RESTAssured Tests

Logging and Debugging RESTAssured

The most verbose option you can use to debug your Tests is log().all(). Here is an example:

Response response = given().log().
                        all().when().
                              get("/resource");

This will display all the Request data (including Headers or Cookies if any) and the Response:

Request method:	GET
Request URI:	http://localhost:8081/resource
Proxy:			<none>
Request params:	<none>
Query params:	<none>
Form params:	<none>
Path params:	<none>
Headers:		Accept=*/*
Cookies:		<none>
Multiparts:		<none>
Body:			<none>

HTTP/1.1 200 OK
Content-Type: application/json
content-length: 200

[

    {
        "id": 3,
        "name": "Noelia",
        "organization": "Acme",
        "talks": [
            {
                "id": 4,
                "duration": 20,
                "title": "title"
            }
        ]
    }
]

On the other hand, you might want to activate such verbose logs only if the response code indicates an error (4xx or 5xx status code). For this purpose you can use the option log().ifError():

given()
    .log().ifError()
.when()
    .post("/users")
.then()
    .statusCode(201);

Another selective option log().ifValidationFails(): This option logs the request and response details only if the validation of the response fails. It’s helpful to add this option when you want to see the details of failed assertions. Example usage:

given()
    .log().ifValidationFails()
.when()
    .get("/users/123")
.then()
    .statusCode(200)
    .body("name", equalTo("John Doe"))
    .body("age", greaterThan(18));

Finally, we will mention that you can activate Logging of all Tests where validation fails with just one configuration property:

RestAssured.enableLoggingOfRequestAndResponseIfValidationFails();

Conclusion

By using these techniques, you can debug your REST Assured tests more effectively and identify and fix issues quickly. Debugging is an essential skill for any software developer or tester, and by learning how to debug your REST Assured tests, you can improve the quality of your code and become a more effective developer.

Found the article helpful? if so please follow us on Socials