automatictester

my thoughts on test automation

REST-assured Tutorial

In this short post I’ll show you how to create your very first tests with REST-assured. REST-assured is a Java library which provides extremely easy to use DSL for testing REST services. We will use online API, JSONPlaceholder.

In this ultra simple tutorial, we will use only two files: Maven pom.xml and Java test class.

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>uk.co.automatictester</groupId>
    <artifactId>restassured-tutorial</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>

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

    </dependencies>

</project>

Test class

import com.jayway.restassured.builder.RequestSpecBuilder;
import com.jayway.restassured.builder.ResponseSpecBuilder;
import com.jayway.restassured.specification.RequestSpecification;
import com.jayway.restassured.specification.ResponseSpecification;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import static com.jayway.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.equalTo;

public class PostsTests {

    RequestSpecification reqSpec;
    ResponseSpecification respSpec;

    @BeforeClass
    public void setupSpec() {
        reqSpec = new RequestSpecBuilder().setBaseUri("http://jsonplaceholder.typicode.com").build();
        respSpec = new ResponseSpecBuilder().expectStatusCode(200).build();
    }

    @Test
    public void verifyId() {
        given().spec(reqSpec)
                .when().get("/posts/5")
                .then().spec(respSpec)
                .body("id", equalTo(5));
    }

    @Test
    public void verifyUserId() {
        given().spec(reqSpec)
                .when().get("/posts/5")
                .then().spec(respSpec)
                .body("userId", equalTo(1));
    }

    @Test
    public void verifyTitle() {
        given().spec(reqSpec)
                .when().get("/posts/5")
                .then().spec(respSpec)
                .body("title", equalTo("nesciunt quas odio"));
    }

}

In above example we use RequestSpecBuilder to set base URI for all requests. We also use ResponseSpecBuilder to set expected status code, which is also common for all requests.

Our tests focus on verification of response body of a following request:
http://jsonplaceholder.typicode.com/posts/5

Advertisements

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

%d bloggers like this: