[JAVA] REST API test with REST Assured Part 2

Introduction

I used REST Assured in the previous "REST API test using REST Assured". I wrote an introduction to how to write REST API tests. Last time I only touched it, but this time I will write more practical content.

How to use

About Response

Validate Response using JSONPath

Describe using the analysis method in JSONPath. Request is thrown as usual with given (). Get () or given (). Post (), and Response is analyzed after then ().

    @Test
public void test() {
        given()
                .get("/v1/clients/15930/account-periods")
        .then()
                .body("accountPeriods", hasSize(3))
                .body("accountPeriods.accountPeriodId", hasItems(24216, 24582, 24583))
                .body("accountPeriods[0].accountPeriodId", equalTo(24216))

        ;
    }

hasSize () and hasItem (), ʻequalTo ()are ʻimport static org.hamcrest.Matchers. *; You can call it by setting. JSON parsing with JSONPath basically follows from Root with . as a delimiter. There are various notations, so please try them.

Parse the JSON of Response Body and receive the specific part as a String

In the previous example, Response is directly asserted, but I will write a method to receive it as a variable once.

    @Test
public void test() {
        String response = get("/v1/clients/15930/account-periods").asString(); //Receive ResponseBody as a String
        int id = from(response).get("accountPeriods[0].accountPeriodId"); //Parse as JSON and get the target with JSONPath
        out.println(id);
    }

Throw a Request directly with get () and receive the result as a string as ʻasString (). After that, parse it as JSON with from (String), and then use JSONPath with get ()` to get the specified part in JSON.

You can write it in this way, such as when you want to include the value of Response in the next Request.

About Request

Next, I will explain the variation of how to throw Request.

Use path parameters (variadic arguments)

The first method of using the path parameter of the URI is to pass the variable part with a variable length argument. Follow the Official documentation on path parameters.

Here, the log of Request is also spit out.

    @Test
public void path parameter() {
        given()
                .log()
                .all()
                .get("/v1/clients/{id}/account-periods", 15930)
        .then()
                .body("accountPeriods", hasSize(3))
        ;
    }

In the part with get ("/ v1 / clients / {id} / account-periods ", 15930), the argument 15930 is applied to the part with{id}, and / v1 / clients / 15930 The GET method is called for / account-periods.

Since it is set to log (). All (), the log of Requst is output to the standard output and can be confirmed.

Use path parameters (use Map)

Explains how to pass path parameters in Map. Pass Map as an argument as get ("/ v1 / clients / {id} / {api} ", params), get the value of Map with variables ʻid, ʻapi in the path as Key, and pass Is assembled.

    @Test
public void Path Parameta Map() {
        Map<String, String> params = new HashMap<String, String>();
        params.put("id", "15930");
        params.put("api", "account-periods");
        given()
                .log()
                .all()
                .get("/v1/clients/{id}/{api}", params)
        .then()
                .body("accountPeriods", hasSize(3))
        ;
    }

Include JSON in Request Body

Explains how to include JSON in Request, which is common in REST API. Just like Official documentation about Request Body, you can set JSON in the argument of body (). is. Since the log output of Request is also set, you can check whether it was actually POSTed.

    private static String postJson = "{sessionId: \"abc\"}";

    @Test
public void JSON transmission test() {
        given()
                .log() //Log out
                .all() //All
                .body(postJson) //Request Body settings
                .post("/v1/clients/15930/account-periods")

        .then()
                .log()
                .body()
        ;
    }

At the end

We have introduced some samples using REST Assured. In addition to this, various patterns will actually be needed for testing, but the API is an intuitive name and Official Document Assured / wiki / Usage) is so rich that you won't get lost.

SessionFilter that performs routine processing at the time of authentication, and [Logging](https: /) that was also used in the sample /github.com/rest-assured/rest-assured/wiki/Usage#logging), [JSON Schema Validation](https://github.com/rest-assured/rest-assured/wiki/Usage#json-schema- validation) etc. are also explained. Please read the official documentation and give it a try.

Recommended Posts

REST API test with REST Assured Part 2
REST API testing with REST Assured
Test Web API with junit
Customize REST API error response with Spring Boot (Part 2)
Customize REST API error response with Spring Boot (Part 1)
Spring with Kotorin --4 REST API design
Build REST API with Apache2 + Passenger + Sinatra.
Compatible with Android 10 (API 29)
I created an api domain with Spring Framework. Part 2
SaveAsBinaryFile with Spark (Part 2)
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
[Rails] Test with RSpec
Test Nokogiri with Rspec.
I created an api domain with Spring Framework. Part 1
Automatically test with Gauge
Load test with JMeter
Unit test with Junit.
Implement REST API with Spring Boot and JPA (Application Layer)
Implement REST API with Spring Boot and JPA (Infrastructure layer)
What I was addicted to with the Redmine REST API
Implement REST API with Spring Boot and JPA (domain layer)
Implement a simple Rest API with Spring Security & JWT with Spring Boot 2.0
Bean mapping with MapStruct Part 1
Create XML-RPC API with Wicket
Bean mapping with MapStruct Part 3
Bean mapping with MapStruct Part 2
Use Bulk API with RestHighLevelClient
Test Active Strage with RSpec
API creation with Rails + GraphQL
Introduction to EHRbase 2-REST API
Remote debugging with Gradle test
Test GraphQL resolver with rspec
Link API with Spring + Vue.js
Implement a simple Web REST API server with Spring Boot + MySQL
I implemented Rails API with TDD by RSpec. part2 -user authentication-
[Beginner] Let's write REST API of Todo application with Spring Boot