RequestSpicification - Response Specification -Pojo

 Example:

public class RequestResponseSpecificationWithBuilder {


static RequestSpecification reqSpec;

static ResponseSpecification resSpec;


public RequestSpecification requestSpecTestBuilder() {

reqSpec = new RequestSpecBuilder().setBaseUri("https://gorest.co.in/public/v2/users/7466205")

.setContentType(ContentType.JSON).build();


return reqSpec; }

public ResponseSpecification responseSpecTestBuilder() {


resSpec = new ResponseSpecBuilder().expectBody("name", equalTo("Indra Pillai")).expectStatusCode(200).build();

return resSpec;

}


@Test

public void reqResSpecBuilder() {

Response  res = given().spec(requestSpecTestBuilder()).log().all().when().get();

System.out.println(res.asPrettyString());

ObjectMapper mapper = new ObjectMapper();

try { //De-serialzation - converting Json to Pojo

ResponseGoRestUserPojo  tp = mapper.readValue(res.getBody().asString(), ResponseGoRestUserPojo.class);

String name=tp.getName();

System.out.println(name);

} catch (JsonProcessingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

res.then().spec(responseSpecTestBuilder());

}


}

Pojo :

package RequestSpecification;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor

public class ResponseGoRestUserPojo {

private String id;
private String name;
private String email;
private String gender;
private String status;

}

Examples:



public class RequestResponseSpecificationbuilder {


public RequestSpecification requestSpecificationBuildr() {

RequestSpecification  reqSpec = new RequestSpecBuilder()

.setBaseUri("https://test.api.amadeus.com/v1/security/oauth2/token")

.setContentType(ContentType.URLENC)

.addFormParam("grant_type", "client_credentials")

.addFormParam("client_id", "3WEebG9lWQQqwnRbngz5zkhwKtmEXVKs")

.addFormParam("client_secret", "88uhUgeGJX31a0aa").build();

return reqSpec;


}

@Test

public void getToken() {

Response  res = given().spec(requestSpecificationBuildr()).log().all()

.when().post();

System.out.println(res.asPrettyString());

}


}




Comments

Popular posts from this blog

Implicit and Explicit Waits,FluentWait,PageLoadTimeOut

A Interview Questions- selenium