Swagger Petstore: Swagger UI
Cài đặt thư viện
Trong file pom.xml:
- Tạo cặp thẻ <dependencies></dependencies> bên dưới cặp thẻ <properties></properties>
- Truy cập Maven Repository
Tìm và thêm 3 thư viện:
- Rest-Assured: là 1 thư viện Java DSL được xây dựng trên nền HTTP Builder (thư viện tạo HTTP request), cho phép thực hiện gửi request và kiểm tra response. Nói đơn giản là một thư viện được xây dựng sẵn để việc thực hiện kiểm thử API trở nên dễ dàng hơn.
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>4.4.0</version>
<scope>test</scope>
</dependency>
- TestNG: là 1 framework test, nó quản lý việc tạo test suite / test case, thứ tự run test và report sau khi run test xong.
- Json-simple: là thư viện xử lý JSON, có JSONObject để viết các đối tượng JSON.
Thêm user mới
Method: POST
Trình tự của 1 Post Request phải làm là:
- Tạo ra Object
- Set giá trị cho các fields của Object đó
- Call API
- Check response sau khi gọi API
Chú ý mục Curl
Trong file Test_Post_Request.java
@Test
public void test_post_01(){
}
Tạo ra Object:
Map<String, Object> map = new HashMap<String, Object>();
Set giá trị cho các fields của Object đó
map.put("id",999);
map.put("username","lamthungng");
map.put("firstName", "Lam Thu");
map.put( "lastName", "Nguyen");
map.put("email", "n2lamthu@gmail.com");
map.put( "password", "123456@H");
map.put( "phone", "+8435679113");
map.put( "userStatus", 1);
System.
out
.println(map);
⇒ {firstName=Lam Thu, lastName=Nguyen, password=123456@H, userStatus=1, phone=+8435679113, id=999, email=n2lamthu@gmail.com, username=lamthungng}JSONObject request = new JSONObject(map);
System.
out
.println(request.toJSONString());
Sử dụng toJSONString() thì in ra kết quả {"firstName":"Lam Thu","lastName":"Nguyen","password":"123456@H","userStatus":1,"phone":"+8435679113","id":999,"email":"n2lamthu@gmail.com","username":"lamthungng"}
Call API
Vì body ở phần yêu cầu là 1 Object Json nên khi gửi request POST, cần phải viết rõ contentType là
application/json
Viết là
contentType(ContentType.JSON)
hoặc
header("Content-Type","application/json")
Còn accept để thông báo loại định dạng mà client đọc được
header("accept","application/json")
Check response sau khi gọi API
given().
contentType(ContentType.JSON).
body(request.toJSONString()).
when().
then().
statusCode(200).
log().all();
Để hiểu phần
log().all()
, tham khảo thêm [Bài 10] Một số techniques liên quan đến log trong Rest-assured | GiangTester BlogĐể thay đổi thông tin user ta thực hiện tương tự, chỉ cần đổi post() thành put().
Lấy thông tin user vừa tạo và check thông tin
Method: GET
Cách 1:
given
().
header("Content-Type","application/json").
header("accept","application/json").
when().
get("https://petstore.swagger.io/v2/user/lamthungng").
then().
statusCode(200).
body("id",
equalTo
(999)).
body("firstName",
equalTo
("Lam Thu")).
body("username",
equalTo
("lamthungng")).
log().all();}
Dùng hàm equalTo() của hamcrest để check thông tin.
Cách 2:
Response response =
get
("https://petstore.swagger.io/v2/user/lamthungng");
int statusCode = response.getStatusCode();
System.
out
.println(response.getBody().asString());
System.
out
.println(statusCode);
System.
out
.println(response.asString());
System.
out
.println(response.getTime());
System.
out
.println(response.getHeaders());
System.
out
.println(response.getHeader("accept"));
System.
out
.println(response.getHeader("content-type"));
Assert.
assertEquals
(statusCode, 200);
Để delete user ⇒ làm tương tự như get
Response response = delete("https://petstore.swagger.io/v2/user/lamthungng");
int statusCode = response.getStatusCode();
System.
out
.println(response.getBody().asString());
System.
out
.println(statusCode);
System.
out
.println(response.asString());
System.
out
.println(response.getTime());
System.
out
.println(response.getHeaders());
System.
out
.println(response.getHeader("accept"));
System.
out
.println(response.getHeader("content-type"));
Assert.
assertEquals
(statusCode, 200);