Soft Assertions in REST Assured

Profile picture for user devraj

Assertions are used to perform various kinds of validations in the tests and help us to decide whether the test has passed or failed. There are two types of assertions basically Hard and Soft Assert.

Hard Assertions in REST Assured

As the name suggests, these assertions put a strict restriction on the test script in which it is placed. When using hard assertions in the test scripts, your test script will stop executing when the assertion fails, and the test will be failed. Example of hard assert in REST Assured:

    .body("[0]", equalTo("Tarun"))
    .body("[0].user.screen_name", equalTo("tgoswami013"))
    .body("[0].user.followers_count", equalTo(19));

In above example if first assertion fails. It will not check for remaining 2 assertions.

Soft Assertions in REST Assured

To deal with the disadvantage of Hard Assertions, we have an approach in REST Assured. Above code can be rewritten for soft assertions as:

.then().body("[0]", equalTo("Tarun1"), "[0].user.screen_name", equalTo("tgoswami013"),"[0].user.followers_count", equalTo(19));

In .body() you need to specify all assertions separated by comma. Above code will validation all 3 assertions and then fail the scrit.