REST API testing with REST Assured

Introduction

When I was looking for a tool for automatic testing of REST API, Class Method's article to test REST API with assured, but the version was a little old, so I tried again with the latest version as of January 23, 2017.

What is REST Assured?

The library used this time is REST Assured, and you can write REST API tests in Java like DSL. You can also easily write JSON / XML tests by adding a library.

How to use

Here's how to use it to write a test for a REST API that returns simple JSON.

Add dependency

It is a promised dependency addition. Add the following dependency to pom.xml.

        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>3.0.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>json-path</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

The first is the REST Assured body, the second is the additional library for evaluating the contents of JSON with a path expression, and the third is JUnit. It can be written as JUnit test code, but it uses hamcrest's Macther to express Assertion in a natural way in the DSL provided by REST Assured.

static import postscript

When writing the test code, declare the following static import to write it as a DSL.

import static io.restassured.matcher.ResponseAwareMatcher.*;
import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;

Test code creation

I will actually write the test code. REST Assured actually sends an HTTP Request to the target server, but here we use JSONPlaceholder.

Let's write the following code.

        RestAssured.baseURI = "http://jsonplaceholder.typicode.com/";
        given()
                .get("/posts/2")
        .then()
                .body("userId", equalTo(1))
                .body("id", equalTo(2))
                .body("title", equalTo("qui est esse"))
        ;

I hope you could see the code and somehow understand what you are doing. Start with givent (), access with GET Method with get (url), and after then (), the operation about the result is written, here the JSON of Response is passed with the body () method. The expression is parsed and the Assertion is executed.

The JSON returned when you hit the URL according to the above test code is as follows

{
  "userId": 1,
  "id": 2,
  "title": "qui est esse",
  "body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla"
}

If you want to send the JSON response to the standard output programmatically, call prettyPrint () for the Response as shown below.

        given().get("/posts/2").prettyPrint();

This alone will format the JSON properly and output it to standard output, so you can use it for debugging as it is.

Github

The code actually written has already been pushed to the following Github. https://github.com/shimashima35/RestAssuredSample

At the end

A brief description of REST Assured, a tool for REST API testing. REST Assured builds test code in a method chain, so it has a high affinity with the IDE. I haven't explained the detailed operations here, but I think you can do various things just by trying out the methods provided by the IDE.

Recommended Posts

REST API testing with REST Assured
REST API test with REST Assured Part 2
Spring with Kotorin --4 REST API design
Automatic API testing with Selenium + REST-Assured
Build REST API with Apache2 + Passenger + Sinatra.
Testing with com.google.testing.compile
Compatible with Android 10 (API 29)
Testing model with RSpec
Hello World (REST API) with Apache Camel + Spring Boot 2
[Spring Boot] Get user information with Rest API (beginner)
Implement a simple Rest API with Spring Security with Spring Boot 2.0
Customize REST API error response with Spring Boot (Part 2)
Customize REST API error response with Spring Boot (Part 1)
Create XML-RPC API with Wicket
Test Web API with junit
Implement REST API with Spring Boot and JPA (Application Layer)
Use Bulk API with RestHighLevelClient
API creation with Rails + GraphQL
Streamline Java testing with Spock
Introduction to EHRbase 2-REST API
What I was addicted to with the Redmine REST API
Link API with Spring + Vue.js
Implement REST API with Spring Boot and JPA (domain layer)
Implement a simple Rest API with Spring Security & JWT with Spring Boot 2.0
Implement a simple Web REST API server with Spring Boot + MySQL
[Beginner] Let's write REST API of Todo application with Spring Boot
Micro benchmark made with JFR API
Implement REST API in Spring Boot
FileUpload with Rest on Apache Wicket
CORS support with Angular + Rest (Java)