REST Assured 14 - RequestSpecification
REST Assured 系列汇总 之 REST Assured 14 - RequestSpecification
前言:
我们将了解Rest Assured的RequestSpecification 类,了解它是什么,什么时候用,怎么用。
<!-- REST Assured --><dependency><groupId>io.rest-assured</groupId><artifactId>rest-assured</artifactId><version>3.0.7</version></dependency>
为什么用 RequestSpecification?
仔细观察下面代码,你会发现在@Test 方法中given()方法后有共同的语句。
import org.junit.Test;
import io.restassured.RestAssured;public class WithoutUsingRequestSpecification {@Testpublic void getAllBookings(){// GivenRestAssured.given()// Common baseURI and basePath.baseUri("https://restful-booker.herokuapp.com").basePath("/booking")// When.when().get()// Then.then().statusLine("HTTP/1.1 200 OK");}@Testpublic void getBookingDetailsWithInvalidFirstName(){// GivenRestAssured.given()// Common baseURI and basePath.baseUri("https://restful-booker.herokuapp.com").basePath("/booking").param("firstName", "Rahul")// When.when().get()// Then.then().statusLine("HTTP/1.1 200 OK");}
}
怎样使用RequestSpecification
一个RequestSpecification 有一些详细说明可以用下面方式创建:
RequestSpecification requestSpecification = RestAssured.given();
requestSpecification.baseUri("https://restful-booker.herokuapp.com")
requestSpecification.basePath("/booking");
或则不用多次call RequestSpecification引用,采用下面builder模式:
RequestSpecification requestSpecification = RestAssured.given().baseUri("https://restful-booker.herokuapp.com").basePath("/booking");
我们也可以将一个RequestSpecification引用加到一个request
RestAssured.given(requestSpecification)
OR
RestAssured.given().spec(requestSpecification)
import io.restassured.RestAssured;
import io.restassured.specification.RequestSpecification;
import org.junit.Test;
import org.junit.BeforeClass;
public class WithRequestSpecification {static RequestSpecification requestSpecification;@BeforeClasspublic static void setupRequestSpecification(){requestSpecification = RestAssured.given().baseUri("https://restful-booker.herokuapp.com").basePath("/booking");}@Testpublic void getAllBookings(){// GivenRestAssured.given().spec(requestSpecification)// When.when().get()// Then.then().statusLine("HTTP/1.1 200 OK");}@Testpublic void getBookingDetailsWithInvalidFirstName(){// GivenRestAssured.given(requestSpecification).param("firstName", "Rahul")// When.when().get()// Then.then().statusLine("HTTP/1.1 200 OK");}
}
given()和 with()
RestAssured类有下面两种方法创建Request specifications
given()
with()
上面方法返回类型都是RequestSpecification,没有啥区别,唯一的区别就是语法。
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;public class RequestSpecificationExample {public static void main(String[] args) {// Creating request specification using given()RequestSpecification request1 = RestAssured.given();// Setting Base URIrequest1.baseUri("https://restful-booker.herokuapp.com");// Setting Base Pathrequest1.basePath("/booking");// Creating request specification using with()RequestSpecification request2 = RestAssured.with();// Setting Base URIrequest2.baseUri("https://restful-booker.herokuapp.com");// Setting Base Pathrequest2.basePath("/ping");// You can also use builder pattern as belowRequestSpecification request3 = RestAssured.with();request3.baseUri("https://restful-booker.herokuapp.com").basePath("/ping");}}
REST Assured 14 - RequestSpecification相关推荐
- Python 模块大全(很详细!)
转载:.... Python的模块大全,很全,有详细介绍! 另外附Python两个教程 1. Python详细教程(廖雪峰的官方网站,语言简洁!) 2. Python 进阶教程 (Vamei) 3. ...
- REST Assured 17 - 设置默认的RequestSpecification
REST Assured 系列汇总 之 REST Assured 17 - 设置默认的RequestSpecification 我们可以根据需要创建多个Request Specification,Re ...
- 看板 工具_2019年14种最佳看板工具
看板 工具 A powerful Kanban board is the solution. This excellent project collaboration tool is widely u ...
- 【Kaggle Learn】Python 1-4
[Kaggle Learn]Python https://www.kaggle.com/learn/python 一. Hello, Python A quick introduction to Py ...
- linux下yum错误:[Errno 14] problem making ssl connection Trying other mirror.
所有的base 都要取消注释 mirrorlist 加上注释 另外所有的enable都要设为零 目录 今天是要yum命令安装EPEL仓库后 yum install epel-release 突然发现y ...
- iPhone 14 与iPhone 13
iPhone 14 与iPhone 13 iPhone14Pro配置曝光:感叹号+4800w像素 | 小米12Ultra 最新套壳图 距离下一代iPhone发布还有半年之久,关于iPhone14系列的 ...
- (14)某工业生产部门根据国家计划的安排, 拟将某种高效率的5台机器,分配给所属的3个工厂A,B,C,各工厂在获得这种机器后,可以为国家盈利的情况如表4-10所示。
问题描述: (14)某工业生产部门根据国家计划的安排, 拟将某种高效率的5台机器,分配给所属的3个工厂A,B,C,各工厂在获得这种机器后,可以为国家盈利的情况如表4-10所示.问:这5台机器如何分配给 ...
- Go语言调度器之调度main goroutine(14)
本文是<Go语言调度器源代码情景分析>系列的第14篇,也是第二章的第4小节. 上一节我们通过分析main goroutine的创建详细讨论了goroutine的创建及初始化流程,这一节我们 ...
- 简介SharePoint 2010 14 Hive文件夹
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u012025054/article/details/36018873 简介SharePoint 20 ...
- C++ 笔记(14)— 指针(指针声明、取地址、取值、new/delete、NULL指针、指针运算、指针数组、数组指针、指针传递给函数、从函数返回指针)
1. 声明指针 指针是一个变量,其值为另一个变量的地址,即,内存位置的直接地址.就像其他变量或常量一样,您必须在使用指 针存储其他变量地址之前,对其进行声明. 指针变量声明的一般形式为: type * ...
最新文章
- NVIDIA TensorRT:可编程推理加速器
- Protocol Buffers简明教程
- Oracle Database基础
- 浅淡绿萝2.0和星火计划
- java子类和父类有相同成员_Java -- 父类和子类拥有同名的成员变量的情况
- 解决: Your ApplicationContext is unlikely to start due to a @ComponentScan of the default
- 将多选框中的值,用String接收,并用‘,’隔开,到后台去循环这个数据
- SQL Server 2017:列存储就地更新
- 深度解读SSH免密登录
- [转载] vim风格设置
- LabVIEW中调用Halcon
- 知乎张瑞: 浅析机器学习理论与实践 | 业界对话
- html5学习开发指南
- 智能制造的生产运营管理
- 在线电子书阅读微信小程序 毕业设计(2)分类
- wav2lip:Accurately Lip-syncing Videos In The Wild
- 如何在matlab中表示e,Matlab中表达e的操作方法介绍
- ARM AArch32和AArch64通用寄存器、状态寄存器
- 好桌道壁纸桌面右键菜单如何去掉
- 深入理解Lustre文件系统-第5篇 LDLM:锁管理者
热门文章
- 中国IT公司百强排名
- IEEE802模型与协议标准
- 递归求平均数|理解|讲解| c语言
- Docker的镜像管理及配置加速器
- BlueCoat ProxySG Attack Detection功能
- delphi mysql 加密_Delphi纯代码连SQLite数据库,同时支持数据库的加密解密
- 方正飞鸿智能信息平台产品白皮书(三)
- [转][酷酷的滕]我爱你语录
- wx.scanCode(Object object)使用详解
- ubuntu安装搜狗拼音输入法及安装后没有中文解决办法