RestAssured #8 - Introduction to Rest Assured

RestAssured #8 - Introduction to Rest Assured

Introduction to Rest Assured | RestAssured Class

Introduction to Rest Assured

REST-Assured is an open-source Java library that simplifies the testing of RESTful web services. It is highly influenced by testing techniques used in dynamic languages such as Ruby and Groovy.

REST-Assured wraps the functionality of the underlying HTTP client library, such as Apache HTTP Client or OkHttp, and provides a simplified, more human-readable API for writing test scripts. It allows you to send HTTP requests (GET, POST, PUT, DELETE, etc.) to a web service and validate the response using a fluent API.

REST-Assured is widely used in the industry for testing RESTful web services, it is easy to use, flexible, and can be integrated with other testing frameworks.

It is a Java DSL for simplifying the testing of REST-based services built on top of HTTP Builder.

What is DSL?

DSL (Domain-Specific Language) is a type of programming language that is designed to be used for a specific domain or task. It is typically a simpler and more specialized than a general-purpose programming language.

REST-Assured, the fluent API is a form of DSL that allows developers to express the desired behaviour of a RESTful web service in a way that is natural and easy to understand.

What is fluent API?

A fluent API (Application Programming Interface) is an API that uses a chain of method calls, with each call returning an object that can be used to make further method calls. The goal of a fluent API is to make the code more readable and intuitive by providing a more natural way of expressing the desired behaviour.

Fluent APIs are characterized by their use of method chaining, where the result of one method call is passed as the input to the next method call, allowing multiple operations to be chained together in a single statement. This results in a more compact and readable syntax.

For example, a fluent API for creating a new user might look like this:

userService.create()
    .name("John Doe")
    .email("johndoe@example.com")
    .age(30)
    .execute();

In this example, the create() method is called first, which returns an object that can be used to set the name, email, and age of the user, and then the execute() method is called to send the request. The use of method chaining makes it clear what operations are being performed and the order in which they are being performed, which makes the code more readable and less error-prone.

What is syntactic sugar?

Syntactic sugar is a term used to describe language features or constructs that make the code more readable or expressive but don't add any new functionality. These features are often referred to as "syntactic sugar" because they make the code "sweeter" to read and write, but they don't change what the code does.

Features of Rest Assured

Below are key features of Rest-Assured:

  1. Simple, fluent API: REST-Assured provides a simple, human-readable API for creating and validating HTTP requests and responses. This makes it easy to write test scripts that are easy to read and understand.

  2. Support for various HTTP methods: REST-Assured supports all common HTTP methods (GET, POST, PUT, DELETE, etc.) and allows you to send requests and validate the responses.

  3. JSON and XML support: REST-Assured provides built-in support for working with JSON and XML, allowing you to easily parse and validate the response body.

  4. Validation and assertion: REST-Assured provides a set of methods for validating the response code, headers, and body, allowing you to assert that the response meets your expectations easily.

  5. Support for authentication: REST-Assured supports various types of authentications such as basic auth, OAuth, and Bearer tokens.

  6. Support for proxy and SSL: REST-Assured allows you to configure your test scripts to work with a proxy server or a secure SSL connection.

  7. Support for filters: REST-Assured allows you to use filters on requests and responses, this can be useful for logging, adding headers or for any other purposes.

  8. Support for multipart file uploads: REST-Assured allows you to easily send multipart file uploads in your requests.

  9. Support for cookies: REST-Assured allows you to work with cookies, it has built-in methods for reading and writing cookies, and for validating the cookie's value.

  10. Support for async and parallel requests: REST-Assured allows you to send async and parallel requests and supports a callback mechanism to handle the response.

  11. Support for different assertion libraries: REST-Assured allows you to use different assertion libraries such as Hamcrest, AssertJ and TestNG assertions.

  12. Support for different test frameworks: REST-Assured can be integrated with other testing frameworks such as JUnit, TestNG, or Cucumber.


Rest Assured Class

Before we see how to use Rest Assured for sending an HTTP requests and validate responses, it will be good to know are different types of methods defined in this class.

Package - io.restassured.RestAssured (https://javadoc.io/doc/io.rest-assured/rest-assured/latest/index.html)

Below are common fields defined in the RestAssured class

  • static AutheticationScheme authentication -> Set an authentication scheme that should be used for each request.

  • static String baseURI - The base URI that's used by REST assured when making requests if a non-fully qualified URI is used in the request.

  • static String basePath - A base path that's added to the baseURI by REST assured when making requests.

  • static RestAssuredConfig config - Define a configuration

  • static RequestSpecification requestSpecification - Specify a default request specification that will be sent with each request.

  • static ResponseSpecification responseSpecification - Specify a default response specification that will be sent with each request.

  • static String sessionId - Set the default session id value that'll be used for each request.

  • static boolean urlEncodingEnabled - Specifies if Rest Assured should url encode the URL automatically.

Below are common methods defined in RestAssured Class

  1. HTTP Request Methods

    • static Response get()

    • static Response post()

    • static Response put()

    • static Response delete()

    • static Response head()

    • static Response options()

    • static Response patch()

    • static Response request() - It has Method enum with HTTP methods like POST, GET, DELETE

  2. Authentication Support

    • static AuthenticationScheme basic()

    • static AuthenticationScheme certificate()

    • static AuthenticationScheme digest()

    • static AuthenticationScheme form()

    • static AuthenticationScheme oauth2()

  3. Support Methods

    • static RequestSpecification given()

    • static RequestSender when()

    • static ResponseSpecification expect(0

    • static RequestSpecification with()

Below are common classes which are returned by methods

  • Response - It is an interface defined in package io.restassured.response. It stores the response of a request made by REST Assured.

    1. Common methods defined or inherited in Response interface are -

      • int getStatusCode()

      • Headers getHeaders()

      • ResponseBody getBody()

  • RequestSpecification - It is an interface defined in package io.restassured.specification which extends RequestSender interface. It allows you to specify how the request will look like.
  • Common methods defined are

    • RequestSpecification and() - Syntactic sugar method which is used to improve readability of code

    • RequestSpecification basePath() - Set the basePath property of the RequestSpecification instead of using static field RestAssured.basePath.

    • RequestSpecification header() - Specify a header that'll be sent with the request

    • RequestSpecification body(File body) - Specify file content that'll be sent with the request.

    • RequestSpecification body(Object object) - Specify an Object request content that will automatically be serialized to JSON or XML and sent with the request.

    • RequestSpecification body(String body) - Specify a String request body

    • RequestSpecification cookie(Cookie cookie)

    • RequestSpecification param()

    • ResponseSpecification then() - Returns the response specification so that you can setup the expectations on the response.

  • ResponseSpecification - It is an interface defined in package io.restassured.specification. Allows you to specify how the expected response must look in order for a test to pass.
  • Common methods defined are

    • ResponseSpecification header() - Expect that a response header matches the supplied header name and hamcrest matcher.

    • ResponseSpecification statusCode()

    • ResponseSpecification statusLine()

    • ResponseSpecification then()

    • ResponseSpecification body() - Expect that the response body conforms to one or more Hamcrest matchers.

    • ResponseSpecification contentType() - Set the response content type to be contentType.

Did you find this article valuable?

Support SUBODH SINGH by becoming a sponsor. Any amount is appreciated!