Rest-Assured 学习笔记
Rest-Assured 学习笔记
Rest-Assured 学习笔记
安装 Rest-Assured
<dependencies>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>4.3.0</version>
<scope>test</scope>
</dependency>
</dependencies>
静态引入方式
import static io.restassured.RestAssured.*;
GET 请求示例
package com.test.rest_asslured;
import static io.restassured.RestAssured.*;
public class ApiTest {
public static void main(String[] args) {
given().
when().get("https://httpbin.org/get?name=zhangsan").
then().log().all();
}
}
使用 queryParam 的 GET 请求
package com.test.rest_asslured;
import static io.restassured.RestAssured.*;
public class ApiTest {
public static void main(String[] args) {
given().
queryParam("name", "zhangsan").
when().get("https://httpbin.org/get").
then().log().all();
}
}
引入 TestNG
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.7.1</version>
<scope>test</scope>
</dependency>
Rest-Assured 语法
When-Then 是类似于驱动开发中定义的一种结构,Given 在某种场景下,When 发生什么事情,Then 产生什么结果。
- given 设置测试预设(预设请求头,请求参数,请求体,cookies等等)
- when 所要执行的操作(GET, POST 等)
- then 解析结果,断言等。
POST 请求示例
@Test
public void ApiPostTest() {
given().
contentType("application/x-www-form-urlencoded;charset=UTF-8").
formParam("name", "张三").
when().
post("https://httpbin.org/post").
then().
log().body();
}
使用 Jackson Databind
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.17.2</version>
</dependency>
Java对象转Json
@Test
public void objectToJson() {
// 创建一个Java对象
Member member = new Member();
member.setName("张三");
member.setAge(30);
// 使用RestAssured发送POST请求,并将Java对象转换为JSON
given().
contentType(ContentType.JSON).
body(member).
when().
post("https://httpbin.org/post").
then().
log().all();
}
HashMap转Json
@Test
public void hashMapToJson() {
// 创建一个HashMap
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("name", "张三");
hashMap.put("age", "30");
// 使用RestAssured发送POST请求,并将HashMap转换为JSON
given().
contentType(ContentType.JSON).
body(hashMap).
when().
post("https://httpbin.org/post").
then().
log().all();
}
断言机制
TestNG 提供了基本的断言机制,但通常使用 Hamcrest 断言框架,它在 Rest-Assured 中已经集成。
import static org.hamcrest.Matcher.*;
JSON 参数示例
{
"lotto": {
"lottoId": 5,
"winning-numbers": [2, 45, 34, 23, 7, 5, 3],
"winners": [
{
"winnerId": 23,
"numbers": [2, 45, 34, 23, 3, 5]
},
{
"winnerId": 54,
"numbers": [52, 3, 12, 11, 18, 22]
}
]
}
}
XML 参数示例
<shopping>
<category type="groceries">
<item>Chocolate</item>
<item>Coffee</item>
</category>
<category type="supplies">
<item>Paper</item>
<item quantity="4">Pens</item>
</category>
<category type="present">
<item when="Aug 10">Kathryn's Birthday</item>
</category>
</shopping>
JSON 断言解析示例
@Test
public void assertJson() {
given().
when().
get("https://1857cad9-61db-44a6-b13f-6f518f76ba1d.mock.pstmn.io/json").
then().
// json断言
// 断言lotto对象的lottoId属性值为5
assertThat().body("lotto.lottoId", equalTo(5));
// 断言winners数组中包含winnerId为23和54的对象
assertThat().body("lotto.winners.winnerId", hasItems(23, 54));
// 通过索引访问winners数组中的第一个元素的winnerId属性
assertThat().body("lotto.winners.winnerId[0]", equalTo(23));
// 使用大于断言winners数组中第一个元素的winnerId属性值大于20
assertThat().body("lotto.winners.winnerId[0]", greaterThan(20));
// 使用findAll函数找出所有winnerId大于40的winners对象,并断言第一个元素的winnerId属性值为54
assertThat().body("lotto.winners.findAll{it.winnerId>40}.winnerId[0]", equalTo(54));
// 使用find函数找出第一个winnerId大于40的winners对象,并断言其winnerId属性值为54
assertThat().body("lotto.winners.find{it.winnerId>40}.winnerId", equalTo(54));
// 使用sum函数计算winning-numbers数组中所有数字的和,并断言其值为119
assertThat().body("lotto.winning-numbers.sum()", equalTo(119));
// 使用findAll函数找出所有winnerId大于40的winners对象,并使用sum函数计算这些对象的winnerId属性值的和,并断言其值为54
assertThat().body("lotto.winners.findAll{it.winnerId>40}.winnerId.sum()", equalTo(54));
// 使用max函数找出winning-numbers数组中的最大值,并断言其值为45
assertThat().body("lotto.winning-numbers.max()", equalTo(45));
// 使用min函数找出winning-numbers数组中的最小值,并断言其值为2
assertThat().body("lotto.winning-numbers.min()",
响应体断言 XML 示例
@Test
public void assertJsonXML() {
given().
when().
get("https://1857cad9-61db-44a6-b13f-6f518f76ba1d.mock.pstmn.io/xml").
then().
// 断言category节点第一个item的值为Paper
// 断言category节点中type为'groceries'的子节点的第一个item的值为'Paper'
assertThat().body("shopping.category[1].item[0]", equalTo("Paper"));
// 断言category节点中type为'groceries'的子节点包含item值为'Chocolate'和'Coffee'
assertThat().body("shopping.category.find{it.@type=='groceries'}.item", hasItems("Chocolate", "Coffee"));
// 使用查找所有category节点中type为'groceries'的子节点,并断言包含item值为'Chocolate'和'Coffee'
assertThat().body(".find { it.@type == 'groceries' } ", hasItems("Chocolate", "Coffee"));
}

浙公网安备 33010602011771号