Rest Assured – Example

Rest Assured is one of the nice API for testing the Rest web services. Its easy to learn and simple to implement. I can say that its based on Given when then approach[Gherkin language]

Just include the below dependency in the maven pom.xml

<dependency>
       <groupId>com.jayway.restassured</groupId>
       <artifactId>rest-assured</artifactId>
       <version>2.8.0</version>
       <scope>test</scope>
</dependency>

Here is a simple example,


import com.jayway.restassured.RestAssured;
import com.jayway.restassured.response.Response;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class CustomerRestAssuredTest {

    private final static Logger LOGGER = LoggerFactory.getLogger(CustomerRestAssuredTest .class);

    @Before
    public void setUp() {
       // Replace with the domain name of the service
        RestAssured.baseURI = "http://localhost";
        //Port number
        RestAssured.port = 8080;
        //Service Base Path
        RestAssured.basePath = "/service/";
    }

    @Test
    public void testGetCustomers() {
        Response response = RestAssured.when().get("customers");
        assertEquals(200, response.getStatusCode());
        assertNotNull(response.getBody());
        assertEquals(true, response.getBody().asString().contains("CONTENT_TO_TEST_AGAINST"));
    }
  
    @Test
    public void testGetCustomerById() {
        Response response = RestAssured.when().get("customers").param("id","cust_123");
        assertEquals(200, response.getStatusCode());
        assertNotNull(response.getBody());
        assertEquals(true, response.getBody().asString().contains("CONTENT_TO_TEST_AGAINST"));
    }
     
   @Test
    public void testSaveCustomerDetails() {
       //Create request builder
        RequestSpecBuilder builder = new RequestSpecBuilder();
       //Set the body content
        builder.setBody("{\"name\":\"bala\",\"age\":\"60\"}"); 
        //Set the request content type
        builder.setContentType("application/json; charset=UTF-8");
        //Build the spec
        RequestSpecification requestSpec = builder.build();
        Response response = RestAssured.given().spec(requestSpec ).when().post("customers/save");
        assertEquals(200, response.getStatusCode());
        assertNotNull(response.getBody());
        assertEquals(true, response.getBody().asString().contains("SUCCESS"));
    }
}

Assume that we have a service called customers and its returns the list of customers. The service URL is http://localhost:8080/service/customers

Please refer the above code and inside the setUp method, we just have to initialize the Base URL, port and Base Path

After that in the actual test method, We call RestAssured.when() then followed by HTTP method and the service name. Here in this its a GET service and method name is customers. Here we dont pass any parameters to this service. Thats is.
It will return com.jayway.restassured.response.Response and we have to parse through it or get the response body content and validate it.

This is just a simple example. For more information and examples, please refer http://www.hascode.com/2011/10/testing-restful-web-services-made-easy-using-the-rest-assured-framework/

Advertisements

One thought on “Rest Assured – Example

  1. Pingback: Rest Assured API for Rest Service Integration testing | Bala's Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s