While performing RESTful API Testing, we need to check various services and its endpoints. For performing validation weather functionality is working correctly or not, we need to use assertion. Assertion plays very important role in testing process. We need assertion to check already working functionalty returing true or not. If any assertion returns false then it should be potential bug. In general we do variable, dates, status codes and many other comparisons. Thus, Assertion using AssertJ in RESTful Testing is the need of modern automation framework.
In this blog we are going to see assertion in different way. There is two (Hamcrest, AssertJ) most popular java assertion framework available in the market to use free. Hamcrest is widely used matcher or assertion framework for java but AssertJ is now gaining lot of attention due to its fluent syntax. The main goal of the AssertJ is to improve code readability.
In this blog, we are going to discuss more about AssertJ. As AssertJ is more stable development pattern and more commits when we analyze its commits on the recent months.
Assertion Class Structure:-
AssertJ has the more the simple calss structure for assertion in compare to Hamcrest. AssertJ has one static class that contains all the available assertions.
A simple example:-
AssertJ offers many usefull features for assertion. Some of them are:-
1. Assertion specific to the object undert test type.
2. Soft assertion.
3. String specific assertions.
In above image we can see various chaing assertions which is specific to string type. Only “isNotNull” is base assertion there. We can also use code completion feature for this to achieve object type assertion or Object under Test type assertion.
Assertion specific to Object under Test type :-
AssertJ provides unique feature in which it handles object under test by provdig assertions to that specific object. we don’t need to think more about creating logic for this and seraching for best fitt assertion.
Soft Assertion :-
When it comes to the execution of test cases and failing more than one assertion then “Soft Assertion” comes into the picture. SoftAssertion enables us to collect all the assertion errors and tell us at the end of executing all the assertions. Soft assertion is also available in BDD flavour in AssertJ. It uses “then” keyword instead of “assertThat”.\
String Specific Assertion :-
While performing functional testing on RESTFul API’s we need to handle lots of string objecs. So, AssertJ provides String specific assertions. Some of them are “.startsWith(” “)”, “.contains(” “)” and “.endsWith(” “)”.
Assertj provide one very good feature that is assumption. This feature provides conditional test execution. In this if test executed normally then it works like normal assertion and mark test as pass, if they don’t then test is aborted and marked as ignored.
After reading all these points we cas say Assertion using AssertJ in RESTful Testing is one of the fruitfull choice as the Automation tool. It supports fluent syntax and focuses more on the functionality ratehr than the assertion logic. Its more organized class structure provides central class to look all the various assertion types. It provides more readability to the code.