zoukankan      html  css  js  c++  java
  • RestAssured

    配置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

  • 相关阅读:
    NPOI json转Excel DataTable转Excel ,Excel转DataTable
    sqlhelper;
    C# DataSet数据导入Excel 修正版- .net FrameWork 4.0以上
    asp.net core 教程(七)-异常处理、静态文件
    asp.net core 教程(六)-中间件
    asp.net core 教程(五)-配置
    jQuery_3_过滤选择器
    jQuery_2_常规选择器-高级选择器2
    jQuery_2_常规选择器-高级选择器
    jQuery_2_常规选择器-进阶选择器
  • 原文地址:https://www.cnblogs.com/liehen2046/p/11689276.html
Copyright © 2011-2022 走看看