java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Java接口测试框架Restassured

Java接口测试框架Restassured介绍及常用方法

作者:Yweir

Java接口测试主要用于验证Java应用程序中不同模块或服务之间的交互是否符合预期,这篇文章主要介绍了Java接口测试框架Restassured介绍及常用方法的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下

接口测试框架Restassured介绍

市场上的主流的接口测试框架

接口测试框架【Restassured】

SpringBoot3.X整合

<dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
        </dependency>
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured-all</artifactId>
        </dependency>
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>spring-web-test-client</artifactId>
        </dependency>

快速开始

@SpringBootTest
public class AssuredTest {
    @Test
    public void testBase() {
        RestAssured.given()
                .param("id", 1)
                .when()
                .get("http://127.0.0.1:8082/api/v1/test/detail")
                .then()
                .log()
                .all()
                .statusCode(200);
    }
}

Restassured框架常用方法

基础语法格式

参数配置

示例

 	@Test
    public void testBase() {
        RestAssured.given()
                .queryParam("id", 1)
                .when()
                .get("http://127.0.0.1:8082/api/v1/test/detail")
                .then()
                .log().all()
                .statusCode(200);
    }
 	@Test
    public void testPostForm(){
        RestAssured.given()
                .formParam("mail", "1320801376@sina.com")
                .formParam("pwd","123456")
                .when()
                .post("http://127.0.0.1:8082/api/v1/test/login_form")
                .then()
                .log().all()
                .statusCode(200);
    }
    @Test
    public void testPostJsonHeader(){
        Map<String,String> params = new HashMap<>();
        params.put("title","山海经传奇");
        RestAssured.given()
                .header("token","7a9b7dcbba2443c5a80fbfa62da63e69")
                //.header("Content-Type","application/json")
                .contentType(ContentType.JSON)
                .body(JsonUtil.obj2Json(params))
                .when()
                .post("http://127.0.0.1:8082/api/v1/test/buy")
                .then()
                .log()
                .all()
                .statusCode(200);
    @Test
    public void testFile(){
        RestAssured.given()
                .multiPart(new File("/Users/xdclass/Desktop/测试jmx/id.csv"))
                .when()
                .post("http://127.0.0.1:8082/api/v1/test/upload")
                .then()
                .log().all()
                .statusCode(200);
    }

断言与解析

响应断言

类型断言方法含义
状态码statusCode()响应状态码
响应头header()响应头信息
内容body()内容匹配

内置结果解析

示例

断言测试

    @Test
    public void testAssert(){
        Map<String,String> params = new HashMap<>();
        params.put("title","山海经传奇");
        RestAssured.given()
                .header("token","7a9b7dcbba2443c5a80fbfa62da63e69")
                .header("Content-Type","application/json")
                .body(JsonUtil.obj2Json(params))
                .when()
                .post("http://127.0.0.1:8082/api/v1/test/buy")
                .then()
                .log()
                .all()
                .statusCode(200)
                .body("code", equalTo(1));
    }

结果解析

    @Test
    public void testResponse(){
        Map<String,String> params = new HashMap<>();
        params.put("title","山海经传奇");
        Response response = RestAssured.given()
                .header("token", "7a9b7dcbba2443c5a80fbfa62da63e69")
                //.header("Content-Type","application/json")
                .contentType(ContentType.JSON)
                .body(JsonUtil.obj2Json(params))
                .log().headers()//打印请求头
                .log().body()//打印请求体
                .when()
                .post("http://127.0.0.1:8082/api/v1/test/buy")
                .then()
                .log()
                .all()
                .statusCode(200).extract().response();
        Object data = response.jsonPath().get("data");
        System.out.println(data);
    }

总结 

到此这篇关于Java接口测试框架Restassured介绍及常用方法的文章就介绍到这了,更多相关Java接口测试框架Restassured内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文