Search Tutorials

Rest-Assured Hello World - Getting started using simple example | JavaInUse

Rest-Assured Hello World - Getting started using simple example


In previous chapter we implemented REST web service to return either xml or JSON depending on some user parameter using ContentNegotiation parameter. In this chapter we test the same webservice using Rest-Assured.

What is Rest-Assured ?

Testing and validating REST services in Java is harder than in dynamic languages such as Ruby and Groovy. REST Assured brings the simplicity of using these languages into the Java domain. It eliminates the requirement of using boiler-plate code to test complex API responses, and supports both XML and JSON.

Lets Begin

The pom.xml will be as follows-
<project xmlns="" xmlns:xsi=""

Below we use the Given/When/Then structure that is borrowed from BDD (Behaviour Driven Development). In Given section we declare things like content type or request body. In When section we provide HTTP method and endpoint. In Then section we declare response verification.
package com.javainuse;

import org.hamcrest.Matchers;
import org.junit.Test;
import io.restassured.response.ValidatableResponse;
import static io.restassured.RestAssured.given;

public class TestRestAssured {

    public void restAssuredTest() {
        ValidatableResponse response =
            given().queryParam("type", "json").when()
        System.out.println("Response is - "+response.extract().body().asString());

Now start the REST web service we developed in the previous chapter. Run the above class and the test case gets executed successfully. In this test the REST call is made and the response we check if it contains a particular string.

Download Source Code

Download it - Rest-Assured Hello World

See Also

REST Project to return XML Response
REST Project to return JSON Response