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.
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.
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.
Next, I will explain the variation of how to throw Request.
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.
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))
;
}
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()
;
}
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