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.


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">






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;

    public void setupSpec() {
        reqSpec = new RequestSpecBuilder().setBaseUri("").build();
        respSpec = new ResponseSpecBuilder().expectStatusCode(200).build();

    public void verifyId() {
                .body("id", equalTo(5));

    public void verifyUserId() {
                .body("userId", equalTo(1));

    public void verifyTitle() {
                .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:


Leave a Reply

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

You are commenting using your 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: