Rest-assured basic usage

Quoted from Rest-assured official website, Testing and validating REST services in Java is harder than in dynamic languages such as Ruby and Groovy. REST Assured brings the simplicity of using these languages into the Java domain.

First of all, add dependencies into mvn pom.xml. The first dependency is the basic one, and the second is for json data processing.

<dependency>  

<groupId>io.rest-assured</groupId>  

<artifactId>rest-assured</artifactId>  

<version>3.2.0</version>  

</dependency>  

<dependency>  

<groupId>io.rest-assured</groupId>  

<artifactId>json-path</artifactId>  

<version>3.2.0</version>  

</dependency>  

<dependency>

<groupId>io.rest-assured</groupId>

<artifactId>rest-assured</artifactId>

<version>3.2.0</version>

</dependency>

<dependency>

<groupId>io.rest-assured</groupId>

<artifactId>json-path</artifactId>

<version>3.2.0</version>

</dependency>

There are two way access uri to RestAssured for calling service. Usercan use the code below to set base uri for global request, so that user can just give partial uri to send request.

The static method given() will return an object of RequestSpecification type, it will contain all global setting for request.

RestAssured.baseURI="http://localhost:8089";  

RequestSpecification uncompletedRequest=given();//here just define a object to accept the return of given()  

RestAssured.baseURI="http://localhost:8089";

RequestSpecification uncompletedRequest=given();//here just define a object to accept the return of given()

For authentication, Rest-assured provide several way to do it. The basic way is shown as below, and can define authentication for all requests also.

given().auth().basic("username", "password"); //will return an object of RequestSpecification type for chain call  

RestAssured.authentication = basic("username", "password");//for all request  

given().auth().basic("username", "password"); //will return an object of RequestSpecification type for chain call

RestAssured.authentication = basic("username", "password");//for all request

(we use preemptive basic authentication for jira api authentication.)

RestAssured.authentication=preemptive().basic(username,password);  

RestAssured.authentication=preemptive().basic(username,password);

If the service needs ssl certification, the code below is necessary.

RestAssured.useRelaxedHttpsValidation();  

RestAssured.useRelaxedHttpsValidation();

Then there are several optional steps preparing for send a request.

//easy to know that this to set contenttype  

uncompletedRequest = uncompletedRequest.contentType(contentType);  

// this is to set path parameters with a map object following the pattern given in uri  

// for example: uri is http://localhost:8089/greeting?name={name}, the pathParams must be {'name': 'Zhang, San'}  

// pathParams has more or less params than the uri pattern given is not allowed.  

uncompletedRequest = uncompletedRequest.pathParams(pathParams);//there is also method pathParam() to set single key-value param.  

//in this way, all key-value pair will be added into path as params  

uncompletedRequest = uncompletedRequest.params(params);////there is also method param() to set single key-value param.  

// as the method shown, this method is to set request body for post and put request.  

uncompletedRequest = uncompletedRequest.body(body);  

//easy to know that this to set contenttype

uncompletedRequest = uncompletedRequest.contentType(contentType);

// this is to set path parameters with a map object following the pattern given in uri

// for example: uri is http://localhost:8089/greeting?name={name}, the pathParams must be {'name': 'Zhang, San'}

// pathParams has more or less params than the uri pattern given is not allowed.

uncompletedRequest = uncompletedRequest.pathParams(pathParams); //there is also method pathParam() to set single key-value param.

//in this way, all key-value pair will be added into path as params

uncompletedRequest = uncompletedRequest.params(params);////there is also method param() to set single key-value param.

// as the method shown, this method is to set request body for post and put request.

uncompletedRequest = uncompletedRequest.body(body);

Next step is to send request.

// four method relevant to for request type: get, post, put and delete.  

// get response for data processing.  

Response response=uncompletedRequest.get(uri);//if BaseURI is set. here we can pass partial uri like "/greeting"  

// four method relevant to for request type: get, post, put and delete.

// get response for data processing.

Response response=uncompletedRequest.get(uri);//if BaseURI is set. here we can pass partial uri like "/greeting"

Rest-assured provide simple way to do data validation.

//{"lotto":{"lottoId":5,"name":"Li, Si"}} just for simple example  

response.then().statusCode(200).body("lotto.lottoId");  

//{"lotto":{"lottoId":5,"name":"Li, Si"}} just for simple example

response.then().statusCode(200).body("lotto.lottoId");


Also we can get many details by calling methods of Response

response.getStatusCode();  

response.getBody();  

response.getCookies();  

response.getBody().jsonPath();//for some un-standard service, this is a good way to analyze the received json data.  

response.getStatusCode();

response.getBody();

response.getCookies();

response.getBody().jsonPath(); //for some un-standard service, this is a good way to analyze the received json data.


For more details, please refer tohttps://github.com/rest-assured/rest-assured/wiki/Usage

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,499评论 0 10
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,273评论 19 139
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 10,131评论 0 23
  • 因为上个手机掉厕所了,所以不得不买了现在这个新手机。补办卡的话一弄就是4G网的,很耗流量,没用几天就几百兆流量出去...
    爱别离琛妮妮阅读 221评论 0 0
  • 春天又到了,自驾游的人也越来越多,下面小编就给大家介绍一款家用商务两不误的车给大家。空间大、乘坐舒适、机场接人看着...
    道一cy阅读 223评论 0 0