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.
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.
Here's how to use it to write a test for a REST API that returns simple JSON.
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.
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.*;
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
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