配置MAVEN

        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
        </dependency>
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>json-schema-validator</artifactId>
            <version>3.0.2</version>
        </dependency>
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>spring-mock-mvc</artifactId>
            <version>3.0.6</version>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-library</artifactId>
        </dependency>

发送json请求1:

    RequestSpecification request = RestAssured.given().header("Content-Type", "application/json");
    JSONObject requestParams = new JSONObject();
    JSONObject childObject1= new JSONObject();
    JSONObject childObject2 = new JSONObject();
    requestParams.put("contactClass", "ZWSS"); 
    requestParams.put("contactActivity", "0039");
    requestParams.put("contractAccountNumber", "210024144291");
    requestParams.put("text", "Please rate the overall ease of using the website to initiate or make your service request");
    requestParams.put("contactType", "Z1");
    requestParams.put("contactDirection", "1");

    childObject1.put("question", "0001");
    childObject1.put("answer", "01");
    childObject1.put("question", "0002");
    childObject1.put("answer", "02");

    JSONArray jsonArray = new JSONArray();

    jsonArray.put(childObject1);
    jsonArray.put(childObject2);
    requestParams.put("addInfo", jsonArray);

    request.body(requestParams.toString());
    Response response = request.post("https://api-dev.adp.com/api/*/*/*");

发送Json请求2

import com.github.openjson.JSONArray;
import com.github.openjson.JSONObject;
import com.westone.cx.performance.common.AccountConst;
import io.restassured.RestAssured;
import io.restassured.config.SSLConfig;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.hamcrest.Matchers.*;
public class RestAssuredSampler {
    public static void main(String[] args) {
        RestAssured.useRelaxedHTTPSValidation();
        String requestParams = "{\"aaa\":\"11111\",\"bbb\":\"22222\"}";
        RequestSpecification request = RestAssured.given()
//                .config((RestAssured.config().sslConfig(new SSLConfig().relaxedHTTPSValidation())))
                .header("Content-Type", "application/json");
        request.body(requestParams);
        Response response = request.post("http://url");
        response.prettyPrint();
    }
}

Json响应

given().
                headers(headers).
                contentType("application/json").
                body(jsonString).
                when().
                post(url).
                then().
                log().body().
                assertThat().
                statusCode(200).
                body("$.size()",equalTo(1)).
                body("[0].contactMethods.size()",equalTo(2));

Reference

https://github.com/RookieTester/rest-assured-doc/blob/master/2016-12-12-【接口测试】rest-assured用户手册中文版.markdown#JSON(使用JsonPath)

java api

http://static.javadoc.io/io.rest-assured/rest-assured/3.0.1/index.html

posted on 2019-10-16 23:10  liehen2046  阅读(804)  评论(0编辑  收藏  举报