REST Assured 52 - ResponseSpecification – Specify How The Expected Response Must Look Like

REST Assured 系列汇总 之 REST Assured 52 - ResponseSpecification – Specify How The Expected Response Must Look Like

前提条件

添加 rest assured 依赖库

 <!-- REST Assured -->
 <dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>rest-assured</artifactId>
    <version>4.4.0</version>
</dependency>

Why ResponseSpecification?

认真阅读下面的代码,会发同 then() 方法后有一些通用的断言语句。

import org.hamcrest.Matchers;
import org.junit.Test;
 
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
 
public class WithoutUsingResponseSpecification {
    
    
	
	@Test
	public void getAllBookings()
	{
    
    
		// Given
		RestAssured
		  .given()
			 .baseUri("https://restful-booker.herokuapp.com")
		// When
		   .when()
			  .get("/booking")
		// Then
		   .then()
		   .contentType(ContentType.JSON)
		   .time(Matchers.lessThan(5000L))
		   .statusLine("HTTP/1.1 200 OK")
		// To verify booking count
		   .body("size()", Matchers.greaterThan(5));		
	}
	
	@Test
	public void getBookingDetailsWithInvalidFirstName()
	{
    
    
		// Given
		RestAssured
		  .given()
			 .baseUri("https://restful-booker.herokuapp.com")
		// When
		   .when()
			  .get("/booking?firstname=Rahul")
		// Then
		   .then()
		// Repetitive validation as first test above
		   .contentType(ContentType.JSON)
		   .time(Matchers.lessThan(5000L))
		   .statusLine("HTTP/1.1 200 OK")
		// To verify booking count
		   .body("size()", Matchers.equalTo(0));
			
	}
 
}

上面仅是两个测试用例,现实工作中可能会有更多。一组测试用例可能有共同的断言 response 语句。遵循代码简洁规则,不同测试用例重复断言并非好的实践。如果有改动,每个地方都得修改,这非常不便。

为了将通用的断言归类在一起作为一个通用的实体,我们可以用 ResponseSpecification 接口,允许你指定期望的 response 是怎样的。这个接口用一些现成的方法来定义断言,如 status code, content type 等。我们需要用 RestAssured 类中的 expect() 方法来获取 ResponseSpecification 接口的引用。记住, ResponseSpecification 是一个接口,我们不能实例化一个对象。

How to use ResponseSpecification?

// Create a ResponseSpecification 
responseSpecification =  RestAssured.expect();
responseSpecification.contentType(ContentType.JSON);
responseSpecification.statusCode(200);
responseSpecification.time(Matchers.lessThan(5000L));
responseSpecification.statusLine("HTTP/1.1 200 OK");

我们可以用 spec() 方法来添加一个 ResponseSpecification.

...
.then()
.spec(responseSpecification)

实例

import org.hamcrest.Matchers;
import org.testng.annotations.BeforeClass;
import org.junit.Test;
 
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.specification.ResponseSpecification;
 
public class UsingResponseSpecification {
    
    
	
	ResponseSpecification responseSpecification = null;
	
	@BeforeClass
	public void setupResponseSpecification()
	{
    
    
		// Create a ResponseSpecification 
		responseSpecification=  RestAssured.expect();
		responseSpecification.contentType(ContentType.JSON);
		responseSpecification.statusCode(200);
		responseSpecification.time(Matchers.lessThan(5000L));
		responseSpecification.statusLine("HTTP/1.1 200 OK");
		
	}
	
	@Test
	public void getAllBookings()
	{
    
    
		// Given
		RestAssured
		  .given()
			 .baseUri("https://restful-booker.herokuapp.com")
		// When
		   .when()
			  .get("/booking")
		// Then
		   .then()
		   // Just pass ResponseSpecification as below
		   .spec(responseSpecification)
		// To verify booking count
		   .body("size()", Matchers.greaterThan(5));
		
	}
	
	@Test
	public void getBookingDetailsWithInvalidFirstName()
	{
    
    
		// Given
		RestAssured
		  .given()
			 .baseUri("https://restful-booker.herokuapp.com")
		// When
		   .when()
			  .get("/booking?firstname=jim")
		// Then
		   .then()
		   .spec(responseSpecification)
		// To verify booking count
		   .body("size()", Matchers.equalTo(0));
			
	}
 
}

猜你喜欢

转载自blog.csdn.net/wumingxiaoyao/article/details/120576675