REST Assured 系列汇总 之 REST Assured 14 - RequestSpecification

前言:
我们将了解Rest AssuredRequestSpecification 类,了解它是什么,什么时候用,怎么用。

前提:
我们需要用到Rest Assured的依赖包:

<!-- 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");}
}

上面有两个测试用例,但是现实测试中可能会有更多测试用例。一组测试可能会有一些公共的specifications 来创建一个request。在测试用例中有重复的共同的request specifications 并不是好的实践。如果有改变,我们得在每个地方都需要修改。

为了将一些公用的request specifications 聚在一起组合成一个公用的实体,我们可以使用RequestSpecification
RequestSpecification是一个接口,允许你具体指定你的request。 该接口有现成的方法来定义base URL, base path, headers, 等. 我们需要用到RestAssured类中的given() 方法来获取 RequestSpecification的一个引用。记住RequestSpecification 是一个接口,我们不能创建它的实例,RequestSpecificationImpl 是它的实现类。

怎样使用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)

完整的代码
将公共的specification提取出来

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相关推荐

  1. Python 模块大全(很详细!)

    转载:.... Python的模块大全,很全,有详细介绍! 另外附Python两个教程 1. Python详细教程(廖雪峰的官方网站,语言简洁!) 2. Python 进阶教程 (Vamei) 3. ...

  2. REST Assured 17 - 设置默认的RequestSpecification

    REST Assured 系列汇总 之 REST Assured 17 - 设置默认的RequestSpecification 我们可以根据需要创建多个Request Specification,Re ...

  3. 看板 工具_2019年14种最佳看板工具

    看板 工具 A powerful Kanban board is the solution. This excellent project collaboration tool is widely u ...

  4. 【Kaggle Learn】Python 1-4

    [Kaggle Learn]Python https://www.kaggle.com/learn/python 一. Hello, Python A quick introduction to Py ...

  5. linux下yum错误:[Errno 14] problem making ssl connection Trying other mirror.

    所有的base 都要取消注释 mirrorlist 加上注释 另外所有的enable都要设为零 目录 今天是要yum命令安装EPEL仓库后 yum install epel-release 突然发现y ...

  6. iPhone 14 与iPhone 13

    iPhone 14 与iPhone 13 iPhone14Pro配置曝光:感叹号+4800w像素 | 小米12Ultra 最新套壳图 距离下一代iPhone发布还有半年之久,关于iPhone14系列的 ...

  7. (14)某工业生产部门根据国家计划的安排, 拟将某种高效率的5台机器,分配给所属的3个工厂A,B,C,各工厂在获得这种机器后,可以为国家盈利的情况如表4-10所示。

    问题描述: (14)某工业生产部门根据国家计划的安排, 拟将某种高效率的5台机器,分配给所属的3个工厂A,B,C,各工厂在获得这种机器后,可以为国家盈利的情况如表4-10所示.问:这5台机器如何分配给 ...

  8. Go语言调度器之调度main goroutine(14)

    本文是<Go语言调度器源代码情景分析>系列的第14篇,也是第二章的第4小节. 上一节我们通过分析main goroutine的创建详细讨论了goroutine的创建及初始化流程,这一节我们 ...

  9. 简介SharePoint 2010 14 Hive文件夹

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u012025054/article/details/36018873 简介SharePoint 20 ...

  10. C++ 笔记(14)— 指针(指针声明、取地址、取值、new/delete、NULL指针、指针运算、指针数组、数组指针、指针传递给函数、从函数返回指针)

    1. 声明指针 指针是一个变量,其值为另一个变量的地址,即,内存位置的直接地址.就像其他变量或常量一样,您必须在使用指 针存储其他变量地址之前,对其进行声明. 指针变量声明的一般形式为: type * ...

最新文章

  1. NVIDIA TensorRT:可编程推理加速器
  2. Protocol Buffers简明教程
  3. Oracle Database基础
  4. 浅淡绿萝2.0和星火计划
  5. java子类和父类有相同成员_Java -- 父类和子类拥有同名的成员变量的情况
  6. 解决: Your ApplicationContext is unlikely to start due to a @ComponentScan of the default
  7. 将多选框中的值,用String接收,并用‘,’隔开,到后台去循环这个数据
  8. SQL Server 2017:列存储就地更新
  9. 深度解读SSH免密登录
  10. [转载] vim风格设置
  11. LabVIEW中调用Halcon
  12. 知乎张瑞: 浅析机器学习理论与实践 | 业界对话
  13. html5学习开发指南
  14. 智能制造的生产运营管理
  15. 在线电子书阅读微信小程序 毕业设计(2)分类
  16. wav2lip:Accurately Lip-syncing Videos In The Wild
  17. 如何在matlab中表示e,Matlab中表达e的操作方法介绍
  18. ARM AArch32和AArch64通用寄存器、状态寄存器
  19. 好桌道壁纸桌面右键菜单如何去掉
  20. 深入理解Lustre文件系统-第5篇 LDLM:锁管理者

热门文章

  1. 中国IT公司百强排名
  2. IEEE802模型与协议标准
  3. 递归求平均数|理解|讲解| c语言
  4. Docker的镜像管理及配置加速器
  5. BlueCoat ProxySG Attack Detection功能
  6. delphi mysql 加密_Delphi纯代码连SQLite数据库,同时支持数据库的加密解密
  7. 方正飞鸿智能信息平台产品白皮书(三)
  8. [转][酷酷的滕]我爱你语录
  9. wx.scanCode(Object object)使用详解
  10. ubuntu安装搜狗拼音输入法及安装后没有中文解决办法