RestAssured
2024-10-21 09:13:19
配置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
java api
http://static.javadoc.io/io.rest-assured/rest-assured/3.0.1/index.html
最新文章
- Android Activity的4种启动模式详解(示例)
- x265,帧内预测代码分析
- WIN32服务程序(三):完整的服务程序实例
- APP开发和web开发的区别
- SRM 146 DIV1 600
- 河南省第八届ACM程序设计大赛
- [C++] 几行代码生成漂亮图片,数学家就是牛!
- UI design principles
- cesium调用天地图服务
- mongodb3.03开启认证
- Window.onLoad 和 DOMContentLoaded事件的先后顺序
- 【DataStructure】The description of Java Collections Framework
- 【3y】从零单排学Redis【青铜】
- Podfile
- epoll机制详解
- ansible笔记(5):常用模块之文件操作(二)
- ubuntu apt-get 出现NO_PUBKEY的解决方案
- iis子站点
- 基于 Python 和 Pandas 的数据分析(3) --- 输入/输出 基础
- nginx配置目录列表访问权限
热门文章
- 转载Linux常用命令
- 深入分析 Docker 镜像原理
- Cannot create OpenGL context for &#39;eglMakeCurrent&#39;.
- linux--mysql 8.0.16--裸机安装
- VS2012中--查找定义后从未被使用的函数
- js去掉url后某参数【函数封装】
- MYSQL获得查询记录的行号
- mybatic MapperScannerConfigurer的原理
- 集合(六) WeakHashMap与LinkedHashMap
- jquery submit选择器 语法