软件测试接口测试从入门到精通:Java接口自动化_RestAssured

文章目录

15 Java接口自动化 - RestAssured

🎯 本章目标:掌握RestAssured框架,学会用Java编写接口自动化测试。


15.1 RestAssured 简介

什么是RestAssured

RestAssured 是一个用于测试REST API的Java DSL(领域特定语言),让HTTP请求和验证变得简单。

为什么选择RestAssured

#mermaid-svg-GZDzwfU4tIAqLqJ4{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-GZDzwfU4tIAqLqJ4 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-GZDzwfU4tIAqLqJ4 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-GZDzwfU4tIAqLqJ4 .error-icon{fill:#552222;}#mermaid-svg-GZDzwfU4tIAqLqJ4 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-GZDzwfU4tIAqLqJ4 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-GZDzwfU4tIAqLqJ4 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-GZDzwfU4tIAqLqJ4 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-GZDzwfU4tIAqLqJ4 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-GZDzwfU4tIAqLqJ4 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-GZDzwfU4tIAqLqJ4 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-GZDzwfU4tIAqLqJ4 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-GZDzwfU4tIAqLqJ4 .marker.cross{stroke:#333333;}#mermaid-svg-GZDzwfU4tIAqLqJ4 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-GZDzwfU4tIAqLqJ4 p{margin:0;}#mermaid-svg-GZDzwfU4tIAqLqJ4 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-GZDzwfU4tIAqLqJ4 .cluster-label text{fill:#333;}#mermaid-svg-GZDzwfU4tIAqLqJ4 .cluster-label span{color:#333;}#mermaid-svg-GZDzwfU4tIAqLqJ4 .cluster-label span p{background-color:transparent;}#mermaid-svg-GZDzwfU4tIAqLqJ4 .label text,#mermaid-svg-GZDzwfU4tIAqLqJ4 span{fill:#333;color:#333;}#mermaid-svg-GZDzwfU4tIAqLqJ4 .node rect,#mermaid-svg-GZDzwfU4tIAqLqJ4 .node circle,#mermaid-svg-GZDzwfU4tIAqLqJ4 .node ellipse,#mermaid-svg-GZDzwfU4tIAqLqJ4 .node polygon,#mermaid-svg-GZDzwfU4tIAqLqJ4 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-GZDzwfU4tIAqLqJ4 .rough-node .label text,#mermaid-svg-GZDzwfU4tIAqLqJ4 .node .label text,#mermaid-svg-GZDzwfU4tIAqLqJ4 .image-shape .label,#mermaid-svg-GZDzwfU4tIAqLqJ4 .icon-shape .label{text-anchor:middle;}#mermaid-svg-GZDzwfU4tIAqLqJ4 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-GZDzwfU4tIAqLqJ4 .rough-node .label,#mermaid-svg-GZDzwfU4tIAqLqJ4 .node .label,#mermaid-svg-GZDzwfU4tIAqLqJ4 .image-shape .label,#mermaid-svg-GZDzwfU4tIAqLqJ4 .icon-shape .label{text-align:center;}#mermaid-svg-GZDzwfU4tIAqLqJ4 .node.clickable{cursor:pointer;}#mermaid-svg-GZDzwfU4tIAqLqJ4 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-GZDzwfU4tIAqLqJ4 .arrowheadPath{fill:#333333;}#mermaid-svg-GZDzwfU4tIAqLqJ4 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-GZDzwfU4tIAqLqJ4 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-GZDzwfU4tIAqLqJ4 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-GZDzwfU4tIAqLqJ4 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-GZDzwfU4tIAqLqJ4 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-GZDzwfU4tIAqLqJ4 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-GZDzwfU4tIAqLqJ4 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-GZDzwfU4tIAqLqJ4 .cluster text{fill:#333;}#mermaid-svg-GZDzwfU4tIAqLqJ4 .cluster span{color:#333;}#mermaid-svg-GZDzwfU4tIAqLqJ4 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-GZDzwfU4tIAqLqJ4 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-GZDzwfU4tIAqLqJ4 rect.text{fill:none;stroke-width:0;}#mermaid-svg-GZDzwfU4tIAqLqJ4 .icon-shape,#mermaid-svg-GZDzwfU4tIAqLqJ4 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-GZDzwfU4tIAqLqJ4 .icon-shape p,#mermaid-svg-GZDzwfU4tIAqLqJ4 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-GZDzwfU4tIAqLqJ4 .icon-shape .label rect,#mermaid-svg-GZDzwfU4tIAqLqJ4 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-GZDzwfU4tIAqLqJ4 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-GZDzwfU4tIAqLqJ4 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-GZDzwfU4tIAqLqJ4 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} RestAssured优势
DSL语法简洁
与Java生态集成
支持多种验证
与JUnit/TestNG配合
支持JSON/XML


15.2 环境准备

Maven依赖

xml 复制代码
<dependencies>
    <!-- RestAssured -->
    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>rest-assured</artifactId>
        <version>5.3.2</version>
        <scope>test</scope>
    </dependency>
    
    <!-- JUnit 5 -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>5.10.0</version>
        <scope>test</scope>
    </dependency>
    
    <!-- JSON Path -->
    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>json-path</artifactId>
        <version>5.3.2</version>
    </dependency>
</dependencies>

15.3 基础用法

GET请求

java 复制代码
import io.restassured.RestAssured;
import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;

public class ApiTest {
    
    @BeforeAll
    public static void setup() {
        RestAssured.baseURI = "https://jsonplaceholder.typicode.com";
    }
    
    @Test
    public void testGetPost() {
        given()
            .pathParam("id", 1)
        .when()
            .get("/posts/{id}")
        .then()
            .statusCode(200)
            .body("id", equalTo(1))
            .body("title", notNullValue());
    }
    
    @Test
    public void testGetPostsWithParams() {
        given()
            .queryParam("userId", 1)
            .queryParam("_limit", 5)
        .when()
            .get("/posts")
        .then()
            .statusCode(200)
            .body("size()", equalTo(5));
    }
}

POST请求

java 复制代码
@Test
public void testCreatePost() {
    given()
        .contentType(ContentType.JSON)
        .body("{\n" +
            "  \"title\": \"foo\",\n" +
            "  \"body\": \"bar\",\n" +
            "  \"userId\": 1\n" +
            "}")
    .when()
        .post("/posts")
    .then()
        .statusCode(201)
        .body("id", notNullValue())
        .body("title", equalTo("foo"));
}

使用Map传参

java 复制代码
@Test
public void testCreatePostWithMap() {
    Map<String, Object> postData = new HashMap<>();
    postData.put("title", "foo");
    postData.put("body", "bar");
    postData.put("userId", 1);
    
    given()
        .contentType(ContentType.JSON)
        .body(postData)
    .when()
        .post("/posts")
    .then()
        .statusCode(201);
}

15.4 认证

Bearer Token

java 复制代码
@Test
public void testWithToken() {
    given()
        .header("Authorization", "Bearer " + token)
    .when()
        .get("/api/v1/users/me")
    .then()
        .statusCode(200);
}

Basic Auth

java 复制代码
@Test
public void testBasicAuth() {
    given()
        .auth().basic("username", "password")
    .when()
        .get("/api/v1/protected")
    .then()
        .statusCode(200);
}

15.5 响应验证

JSON验证

java 复制代码
@Test
public void testResponseValidation() {
    given()
    .when()
        .get("/posts/1")
    .then()
        .statusCode(200)
        .contentType(ContentType.JSON)
        .body("id", equalTo(1))
        .body("userId", greaterThan(0))
        .body("title", is(not(emptyString())))
        .body("body", containsString("quia"));
}

提取响应数据

java 复制代码
@Test
public void testExtractData() {
    String title = given()
    .when()
        .get("/posts/1")
    .then()
        .statusCode(200)
        .extract()
        .path("title");
    
    System.out.println("Title: " + title);
}

15.6 本章小结

RestAssured核心

#mermaid-svg-Ik98OSGy2VY046pz{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-Ik98OSGy2VY046pz .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-Ik98OSGy2VY046pz .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-Ik98OSGy2VY046pz .error-icon{fill:#552222;}#mermaid-svg-Ik98OSGy2VY046pz .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-Ik98OSGy2VY046pz .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-Ik98OSGy2VY046pz .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-Ik98OSGy2VY046pz .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-Ik98OSGy2VY046pz .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-Ik98OSGy2VY046pz .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-Ik98OSGy2VY046pz .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-Ik98OSGy2VY046pz .marker{fill:#333333;stroke:#333333;}#mermaid-svg-Ik98OSGy2VY046pz .marker.cross{stroke:#333333;}#mermaid-svg-Ik98OSGy2VY046pz svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-Ik98OSGy2VY046pz p{margin:0;}#mermaid-svg-Ik98OSGy2VY046pz .edge{stroke-width:3;}#mermaid-svg-Ik98OSGy2VY046pz .section--1 rect,#mermaid-svg-Ik98OSGy2VY046pz .section--1 path,#mermaid-svg-Ik98OSGy2VY046pz .section--1 circle,#mermaid-svg-Ik98OSGy2VY046pz .section--1 polygon,#mermaid-svg-Ik98OSGy2VY046pz .section--1 path{fill:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-Ik98OSGy2VY046pz .section--1 text{fill:#ffffff;}#mermaid-svg-Ik98OSGy2VY046pz .node-icon--1{font-size:40px;color:#ffffff;}#mermaid-svg-Ik98OSGy2VY046pz .section-edge--1{stroke:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-Ik98OSGy2VY046pz .edge-depth--1{stroke-width:17;}#mermaid-svg-Ik98OSGy2VY046pz .section--1 line{stroke:hsl(60, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-Ik98OSGy2VY046pz .disabled,#mermaid-svg-Ik98OSGy2VY046pz .disabled circle,#mermaid-svg-Ik98OSGy2VY046pz .disabled text{fill:lightgray;}#mermaid-svg-Ik98OSGy2VY046pz .disabled text{fill:#efefef;}#mermaid-svg-Ik98OSGy2VY046pz .section-0 rect,#mermaid-svg-Ik98OSGy2VY046pz .section-0 path,#mermaid-svg-Ik98OSGy2VY046pz .section-0 circle,#mermaid-svg-Ik98OSGy2VY046pz .section-0 polygon,#mermaid-svg-Ik98OSGy2VY046pz .section-0 path{fill:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-Ik98OSGy2VY046pz .section-0 text{fill:black;}#mermaid-svg-Ik98OSGy2VY046pz .node-icon-0{font-size:40px;color:black;}#mermaid-svg-Ik98OSGy2VY046pz .section-edge-0{stroke:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-Ik98OSGy2VY046pz .edge-depth-0{stroke-width:14;}#mermaid-svg-Ik98OSGy2VY046pz .section-0 line{stroke:hsl(240, 100%, 83.5294117647%);stroke-width:3;}#mermaid-svg-Ik98OSGy2VY046pz .disabled,#mermaid-svg-Ik98OSGy2VY046pz .disabled circle,#mermaid-svg-Ik98OSGy2VY046pz .disabled text{fill:lightgray;}#mermaid-svg-Ik98OSGy2VY046pz .disabled text{fill:#efefef;}#mermaid-svg-Ik98OSGy2VY046pz .section-1 rect,#mermaid-svg-Ik98OSGy2VY046pz .section-1 path,#mermaid-svg-Ik98OSGy2VY046pz .section-1 circle,#mermaid-svg-Ik98OSGy2VY046pz .section-1 polygon,#mermaid-svg-Ik98OSGy2VY046pz .section-1 path{fill:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-Ik98OSGy2VY046pz .section-1 text{fill:black;}#mermaid-svg-Ik98OSGy2VY046pz .node-icon-1{font-size:40px;color:black;}#mermaid-svg-Ik98OSGy2VY046pz .section-edge-1{stroke:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-Ik98OSGy2VY046pz .edge-depth-1{stroke-width:11;}#mermaid-svg-Ik98OSGy2VY046pz .section-1 line{stroke:hsl(260, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-Ik98OSGy2VY046pz .disabled,#mermaid-svg-Ik98OSGy2VY046pz .disabled circle,#mermaid-svg-Ik98OSGy2VY046pz .disabled text{fill:lightgray;}#mermaid-svg-Ik98OSGy2VY046pz .disabled text{fill:#efefef;}#mermaid-svg-Ik98OSGy2VY046pz .section-2 rect,#mermaid-svg-Ik98OSGy2VY046pz .section-2 path,#mermaid-svg-Ik98OSGy2VY046pz .section-2 circle,#mermaid-svg-Ik98OSGy2VY046pz .section-2 polygon,#mermaid-svg-Ik98OSGy2VY046pz .section-2 path{fill:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-Ik98OSGy2VY046pz .section-2 text{fill:#ffffff;}#mermaid-svg-Ik98OSGy2VY046pz .node-icon-2{font-size:40px;color:#ffffff;}#mermaid-svg-Ik98OSGy2VY046pz .section-edge-2{stroke:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-Ik98OSGy2VY046pz .edge-depth-2{stroke-width:8;}#mermaid-svg-Ik98OSGy2VY046pz .section-2 line{stroke:hsl(90, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-Ik98OSGy2VY046pz .disabled,#mermaid-svg-Ik98OSGy2VY046pz .disabled circle,#mermaid-svg-Ik98OSGy2VY046pz .disabled text{fill:lightgray;}#mermaid-svg-Ik98OSGy2VY046pz .disabled text{fill:#efefef;}#mermaid-svg-Ik98OSGy2VY046pz .section-3 rect,#mermaid-svg-Ik98OSGy2VY046pz .section-3 path,#mermaid-svg-Ik98OSGy2VY046pz .section-3 circle,#mermaid-svg-Ik98OSGy2VY046pz .section-3 polygon,#mermaid-svg-Ik98OSGy2VY046pz .section-3 path{fill:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-Ik98OSGy2VY046pz .section-3 text{fill:black;}#mermaid-svg-Ik98OSGy2VY046pz .node-icon-3{font-size:40px;color:black;}#mermaid-svg-Ik98OSGy2VY046pz .section-edge-3{stroke:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-Ik98OSGy2VY046pz .edge-depth-3{stroke-width:5;}#mermaid-svg-Ik98OSGy2VY046pz .section-3 line{stroke:hsl(120, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-Ik98OSGy2VY046pz .disabled,#mermaid-svg-Ik98OSGy2VY046pz .disabled circle,#mermaid-svg-Ik98OSGy2VY046pz .disabled text{fill:lightgray;}#mermaid-svg-Ik98OSGy2VY046pz .disabled text{fill:#efefef;}#mermaid-svg-Ik98OSGy2VY046pz .section-4 rect,#mermaid-svg-Ik98OSGy2VY046pz .section-4 path,#mermaid-svg-Ik98OSGy2VY046pz .section-4 circle,#mermaid-svg-Ik98OSGy2VY046pz .section-4 polygon,#mermaid-svg-Ik98OSGy2VY046pz .section-4 path{fill:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-Ik98OSGy2VY046pz .section-4 text{fill:black;}#mermaid-svg-Ik98OSGy2VY046pz .node-icon-4{font-size:40px;color:black;}#mermaid-svg-Ik98OSGy2VY046pz .section-edge-4{stroke:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-Ik98OSGy2VY046pz .edge-depth-4{stroke-width:2;}#mermaid-svg-Ik98OSGy2VY046pz .section-4 line{stroke:hsl(150, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-Ik98OSGy2VY046pz .disabled,#mermaid-svg-Ik98OSGy2VY046pz .disabled circle,#mermaid-svg-Ik98OSGy2VY046pz .disabled text{fill:lightgray;}#mermaid-svg-Ik98OSGy2VY046pz .disabled text{fill:#efefef;}#mermaid-svg-Ik98OSGy2VY046pz .section-5 rect,#mermaid-svg-Ik98OSGy2VY046pz .section-5 path,#mermaid-svg-Ik98OSGy2VY046pz .section-5 circle,#mermaid-svg-Ik98OSGy2VY046pz .section-5 polygon,#mermaid-svg-Ik98OSGy2VY046pz .section-5 path{fill:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-Ik98OSGy2VY046pz .section-5 text{fill:black;}#mermaid-svg-Ik98OSGy2VY046pz .node-icon-5{font-size:40px;color:black;}#mermaid-svg-Ik98OSGy2VY046pz .section-edge-5{stroke:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-Ik98OSGy2VY046pz .edge-depth-5{stroke-width:-1;}#mermaid-svg-Ik98OSGy2VY046pz .section-5 line{stroke:hsl(180, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-Ik98OSGy2VY046pz .disabled,#mermaid-svg-Ik98OSGy2VY046pz .disabled circle,#mermaid-svg-Ik98OSGy2VY046pz .disabled text{fill:lightgray;}#mermaid-svg-Ik98OSGy2VY046pz .disabled text{fill:#efefef;}#mermaid-svg-Ik98OSGy2VY046pz .section-6 rect,#mermaid-svg-Ik98OSGy2VY046pz .section-6 path,#mermaid-svg-Ik98OSGy2VY046pz .section-6 circle,#mermaid-svg-Ik98OSGy2VY046pz .section-6 polygon,#mermaid-svg-Ik98OSGy2VY046pz .section-6 path{fill:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-Ik98OSGy2VY046pz .section-6 text{fill:black;}#mermaid-svg-Ik98OSGy2VY046pz .node-icon-6{font-size:40px;color:black;}#mermaid-svg-Ik98OSGy2VY046pz .section-edge-6{stroke:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-Ik98OSGy2VY046pz .edge-depth-6{stroke-width:-4;}#mermaid-svg-Ik98OSGy2VY046pz .section-6 line{stroke:hsl(210, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-Ik98OSGy2VY046pz .disabled,#mermaid-svg-Ik98OSGy2VY046pz .disabled circle,#mermaid-svg-Ik98OSGy2VY046pz .disabled text{fill:lightgray;}#mermaid-svg-Ik98OSGy2VY046pz .disabled text{fill:#efefef;}#mermaid-svg-Ik98OSGy2VY046pz .section-7 rect,#mermaid-svg-Ik98OSGy2VY046pz .section-7 path,#mermaid-svg-Ik98OSGy2VY046pz .section-7 circle,#mermaid-svg-Ik98OSGy2VY046pz .section-7 polygon,#mermaid-svg-Ik98OSGy2VY046pz .section-7 path{fill:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-Ik98OSGy2VY046pz .section-7 text{fill:black;}#mermaid-svg-Ik98OSGy2VY046pz .node-icon-7{font-size:40px;color:black;}#mermaid-svg-Ik98OSGy2VY046pz .section-edge-7{stroke:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-Ik98OSGy2VY046pz .edge-depth-7{stroke-width:-7;}#mermaid-svg-Ik98OSGy2VY046pz .section-7 line{stroke:hsl(270, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-Ik98OSGy2VY046pz .disabled,#mermaid-svg-Ik98OSGy2VY046pz .disabled circle,#mermaid-svg-Ik98OSGy2VY046pz .disabled text{fill:lightgray;}#mermaid-svg-Ik98OSGy2VY046pz .disabled text{fill:#efefef;}#mermaid-svg-Ik98OSGy2VY046pz .section-8 rect,#mermaid-svg-Ik98OSGy2VY046pz .section-8 path,#mermaid-svg-Ik98OSGy2VY046pz .section-8 circle,#mermaid-svg-Ik98OSGy2VY046pz .section-8 polygon,#mermaid-svg-Ik98OSGy2VY046pz .section-8 path{fill:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-Ik98OSGy2VY046pz .section-8 text{fill:black;}#mermaid-svg-Ik98OSGy2VY046pz .node-icon-8{font-size:40px;color:black;}#mermaid-svg-Ik98OSGy2VY046pz .section-edge-8{stroke:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-Ik98OSGy2VY046pz .edge-depth-8{stroke-width:-10;}#mermaid-svg-Ik98OSGy2VY046pz .section-8 line{stroke:hsl(330, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-Ik98OSGy2VY046pz .disabled,#mermaid-svg-Ik98OSGy2VY046pz .disabled circle,#mermaid-svg-Ik98OSGy2VY046pz .disabled text{fill:lightgray;}#mermaid-svg-Ik98OSGy2VY046pz .disabled text{fill:#efefef;}#mermaid-svg-Ik98OSGy2VY046pz .section-9 rect,#mermaid-svg-Ik98OSGy2VY046pz .section-9 path,#mermaid-svg-Ik98OSGy2VY046pz .section-9 circle,#mermaid-svg-Ik98OSGy2VY046pz .section-9 polygon,#mermaid-svg-Ik98OSGy2VY046pz .section-9 path{fill:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-Ik98OSGy2VY046pz .section-9 text{fill:black;}#mermaid-svg-Ik98OSGy2VY046pz .node-icon-9{font-size:40px;color:black;}#mermaid-svg-Ik98OSGy2VY046pz .section-edge-9{stroke:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-Ik98OSGy2VY046pz .edge-depth-9{stroke-width:-13;}#mermaid-svg-Ik98OSGy2VY046pz .section-9 line{stroke:hsl(0, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-Ik98OSGy2VY046pz .disabled,#mermaid-svg-Ik98OSGy2VY046pz .disabled circle,#mermaid-svg-Ik98OSGy2VY046pz .disabled text{fill:lightgray;}#mermaid-svg-Ik98OSGy2VY046pz .disabled text{fill:#efefef;}#mermaid-svg-Ik98OSGy2VY046pz .section-10 rect,#mermaid-svg-Ik98OSGy2VY046pz .section-10 path,#mermaid-svg-Ik98OSGy2VY046pz .section-10 circle,#mermaid-svg-Ik98OSGy2VY046pz .section-10 polygon,#mermaid-svg-Ik98OSGy2VY046pz .section-10 path{fill:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-Ik98OSGy2VY046pz .section-10 text{fill:black;}#mermaid-svg-Ik98OSGy2VY046pz .node-icon-10{font-size:40px;color:black;}#mermaid-svg-Ik98OSGy2VY046pz .section-edge-10{stroke:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-Ik98OSGy2VY046pz .edge-depth-10{stroke-width:-16;}#mermaid-svg-Ik98OSGy2VY046pz .section-10 line{stroke:hsl(30, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-Ik98OSGy2VY046pz .disabled,#mermaid-svg-Ik98OSGy2VY046pz .disabled circle,#mermaid-svg-Ik98OSGy2VY046pz .disabled text{fill:lightgray;}#mermaid-svg-Ik98OSGy2VY046pz .disabled text{fill:#efefef;}#mermaid-svg-Ik98OSGy2VY046pz .section-root rect,#mermaid-svg-Ik98OSGy2VY046pz .section-root path,#mermaid-svg-Ik98OSGy2VY046pz .section-root circle,#mermaid-svg-Ik98OSGy2VY046pz .section-root polygon{fill:hsl(240, 100%, 46.2745098039%);}#mermaid-svg-Ik98OSGy2VY046pz .section-root text{fill:#ffffff;}#mermaid-svg-Ik98OSGy2VY046pz .section-root span{color:#ffffff;}#mermaid-svg-Ik98OSGy2VY046pz .section-2 span{color:#ffffff;}#mermaid-svg-Ik98OSGy2VY046pz .icon-container{height:100%;display:flex;justify-content:center;align-items:center;}#mermaid-svg-Ik98OSGy2VY046pz .edge{fill:none;}#mermaid-svg-Ik98OSGy2VY046pz .mindmap-node-label{dy:1em;alignment-baseline:middle;text-anchor:middle;dominant-baseline:middle;text-align:center;}#mermaid-svg-Ik98OSGy2VY046pz :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} RestAssured
given
参数设置
Headers
Body
when
HTTP方法
URL
then
状态码验证
Body验证
Header验证
extract
提取数据
传递上下文

课后练习 📝

  1. 基础题 :用RestAssured测试 jsonplaceholder.typicode.com 的接口。

  2. 进阶题:封装一个BaseTest类,包含登录和Token管理。


15.7 下章预告

下一章我们将学习如何从零搭建接口自动化测试框架!


"RestAssured让Java接口测试变得像说话一样自然。"