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

最新文章

  1. Android Activity的4种启动模式详解(示例)
  2. x265,帧内预测代码分析
  3. WIN32服务程序(三):完整的服务程序实例
  4. APP开发和web开发的区别
  5. SRM 146 DIV1 600
  6. 河南省第八届ACM程序设计大赛
  7. [C++] 几行代码生成漂亮图片,数学家就是牛!
  8. UI design principles
  9. cesium调用天地图服务
  10. mongodb3.03开启认证
  11. Window.onLoad 和 DOMContentLoaded事件的先后顺序
  12. 【DataStructure】The description of Java Collections Framework
  13. 【3y】从零单排学Redis【青铜】
  14. Podfile
  15. epoll机制详解
  16. ansible笔记(5):常用模块之文件操作(二)
  17. ubuntu apt-get 出现NO_PUBKEY的解决方案
  18. iis子站点
  19. 基于 Python 和 Pandas 的数据分析(3) --- 输入/输出 基础
  20. nginx配置目录列表访问权限

热门文章

  1. 转载Linux常用命令
  2. 深入分析 Docker 镜像原理
  3. Cannot create OpenGL context for &#39;eglMakeCurrent&#39;.
  4. linux--mysql 8.0.16--裸机安装
  5. VS2012中--查找定义后从未被使用的函数
  6. js去掉url后某参数【函数封装】
  7. MYSQL获得查询记录的行号
  8. mybatic MapperScannerConfigurer的原理
  9. 集合(六) WeakHashMap与LinkedHashMap
  10. jquery submit选择器 语法