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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,854評(píng)論 0 10
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,554評(píng)論 19 139
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,097評(píng)論 0 23
  • 因?yàn)樯蟼€(gè)手機(jī)掉廁所了,所以不得不買(mǎi)了現(xiàn)在這個(gè)新手機(jī)。補(bǔ)辦卡的話一弄就是4G網(wǎng)的,很耗流量,沒(méi)用幾天就幾百兆流量出去...
    愛(ài)別離琛妮妮閱讀 241評(píng)論 0 0
  • 春天又到了,自駕游的人也越來(lái)越多,下面小編就給大家介紹一款家用商務(wù)兩不誤的車(chē)給大家??臻g大、乘坐舒適、機(jī)場(chǎng)接人看著...
    道一cy閱讀 260評(píng)論 0 0

友情鏈接更多精彩內(nèi)容