Sunday 24 November 2019

Rest Assured: Hello World Application


While working with RestAssured, you should be aware of 4 basic function.

given(): You can pass request headers, parameters, cookies and body using this function.

when(): Specify http operations like get, post, put, delete etc.,

then(): Used to validate response, response codes etc., In simple terms, you can add assertions here.

extract(): Used to extract the response to a variable.

You can read it like below.
Given I have request parameters, header and cookies, When I perform this (GET, POST, PUT, DELETE etc.,) then return/validate this response.

Example
Response response = given().header("Accept", "application/json").when().get("api/v1/employees").thenReturn();

App.java
package com.sample.app;

import io.restassured.RestAssured;
import io.restassured.response.Response;

import static io.restassured.RestAssured.given;

public class App {
    public static void main(String args[]) {
        RestAssured.baseURI = "http://localhost:8080";

        Response response = given().header("Accept", "application/json").when().get("api/v1/employees").thenReturn();

        String respStr = response.getBody().asString();
        System.out.println(respStr);
    }
}

Output
[{"id":1,"firstName":"Deepak","lastName":"Moud"},{"id":2,"firstName":"Srinivasa Rao","lastName":"Gumma"},{"id":3,"firstName":"Purna Chandra","lastName":"Rao"},{"id":4,"firstName":"Madhavi Latha","lastName":"Gumma"},{"id":5,"firstName":"Raghava","lastName":"Reddy"},{"id":6,"firstName":"Ram","lastName":"Gurram"},{"id":7,"firstName":"Gopi","lastName":"Battu"},{"id":8,"firstName":"Gopi","lastName":"Gumma"},{"id":9,"firstName":"Radha","lastName":"Krishna"}]




Previous                                                    Next                                                    Home

No comments:

Post a Comment