Few RestAssured (Java) advanced tips for the intermediate skilled

Third Driver
3 min readSep 2, 2021

This is not at all a beginner’s guide. This will only help if you’ve learned or did your home work on RestAssured.

Source: @nixcraft on Twitter

For test automation of APIs, RestAssured and Karate will be the top two choices. With latter requires no programming skills as it bets on Gherkin syntax, RestAssured for me is my choice because of it’s flexibility, scalability and re-usability. RestAssured also follows kind of a BDD syntax but RestAssured is star of the show only when sending request and retrieving response. The rest is Java. For building a test automation framework with RestAssured, we need an IDE (IntellJ IDEA is my preference) and Postman with your API collection to be automated. Of course, Postman can do some testing too but RestAssured allows many other things. Also, not being limited to a particular tools and not depending on it is a good thing for future.

You may figure out better ways of doing things for what I’ll do here. As I’m no expert, I welcome your feedback and open for suggestions. For Selenium test automation we have Page Object Model structure. But for RestAssured I did not see anyone follow such definitive structure. I went on with my own idea of creating the project like Cypress project structure. First we create a Maven-Java project. Then have following packages:

Init.java
  1. Fixtures: to store *.json files for some APIs, with their response taken out from Postman. Will be useful when asserting large responses.
  2. Integration: all the test cases goes here. Also POJO classes used for de-serialization goes here too. For cleaner project, put those POJOs in a directory under the name of the respective API.
  3. Plugins: Init class which is like the heart of the project with instructions for TestNG annotations such as @BeforeClass @BeforeMethod, Log class for logging, Excel data provider class, Data POJO for JSON data provider
  4. Support: File operations like writing responses to JSON, custom request body creation with Linked Hash Maps, custom response assertions.
  5. Additionally, default resources directory in Maven project for storing .properties files used for storing parameters to be sent in request body.

Here’s what a test case will look like…

Test Case

It depends on the domain and the nature of the API collection you’re testing. Therefore, you have to do make the code on your own to tailor your task. There’s no definitive structure.

Below is how the body of the request generated separately for re-usability and clean code.

I used to hate de-serialization with POJO class. But now it’s no big deal because I later learned Alt + Insert keyboard shortcut to generate getters and setters automatically, thanks to IntelliJ. Also, below line was a life saver. It lets you skip unwanted items.

@JsonIgnoreProperties(ignoreUnknown = true)

Hope this will inspire many others to write even better RestAssured projects. Beginners, don’t give up.

--

--