REST Assured 系列汇总 之 REST Assured 14 - RequestSpecification
前言:
我们将了解Rest Assured的RequestSpecification 类,了解它是什么,什么时候用,怎么用。
前提:
我们需要用到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 {
@Test
public void getAllBookings()
{
// Given
RestAssured
.given()
// Common baseURI and basePath
.baseUri("https://restful-booker.herokuapp.com")
.basePath("/booking")
// When
.when()
.get()
// Then
.then()
.statusLine("HTTP/1.1 200 OK");
}
@Test
public void getBookingDetailsWithInvalidFirstName()
{
// Given
RestAssured
.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;
@BeforeClass
public static void setupRequestSpecification()
{
requestSpecification = RestAssured.given()
.baseUri("https://restful-booker.herokuapp.com")
.basePath("/booking");
}
@Test
public void getAllBookings()
{
// Given
RestAssured
.given()
.spec(requestSpecification)
// When
.when()
.get()
// Then
.then()
.statusLine("HTTP/1.1 200 OK");
}
@Test
public void getBookingDetailsWithInvalidFirstName()
{
// Given
RestAssured
.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 URI
request1.baseUri("https://restful-booker.herokuapp.com");
// Setting Base Path
request1.basePath("/booking");
// Creating request specification using with()
RequestSpecification request2 = RestAssured.with();
// Setting Base URI
request2.baseUri("https://restful-booker.herokuapp.com");
// Setting Base Path
request2.basePath("/ping");
// You can also use builder pattern as below
RequestSpecification request3 = RestAssured.with();
request3.baseUri("https://restful-booker.herokuapp.com").basePath("/ping");
}
}