Skip to content

Commit edefef4

Browse files
committed
Request Response specification buider
1 parent 24712cc commit edefef4

File tree

3 files changed

+160
-6
lines changed

3 files changed

+160
-6
lines changed

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/test/java/ParameterizingBasicAPITest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void checkContentType_expectApplicationJson(String zipcode, String countr
5454
}
5555

5656
@Test (priority = 2, dependsOnMethods = "checkStatusCode_expectHttp200", dataProvider = "place")
57-
public void checkPostCodeInResponse_exceptFortMarket(String zipcode, String countryCode, String Place, String State, String Country){
57+
public void checkPostCodeInResponse_expectTrue(String zipcode, String countryCode, String Place, String State, String Country){
5858
given().pathParams("zipcode",zipcode).
5959
pathParams("countryCode",countryCode).
6060
when().
@@ -65,7 +65,7 @@ public void checkPostCodeInResponse_exceptFortMarket(String zipcode, String coun
6565
}
6666

6767
@Test(priority = 3, dependsOnMethods = "checkStatusCode_expectHttp200", dataProvider = "place")
68-
public void checkStateNameInResponse_exceptMaharashtra(String zipcode, String countryCode, String Place, String State, String Country){
68+
public void checkStateNameInResponse_expectTrue(String zipcode, String countryCode, String Place, String State, String Country){
6969
given().pathParams("zipcode",zipcode).
7070
pathParams("countryCode",countryCode).
7171
when().
@@ -76,7 +76,7 @@ public void checkStateNameInResponse_exceptMaharashtra(String zipcode, String co
7676
}
7777

7878
@Test (priority = 4, dependsOnMethods = "checkStatusCode_expectHttp200", dataProvider = "place")
79-
public void checkCountryInResponse_exceptIndia(String zipcode,String countryCode, String place, String State, String Country){
79+
public void checkCountryInResponse_expectTrue(String zipcode,String countryCode, String place, String State, String Country){
8080
given().pathParams("zipcode",zipcode).
8181
pathParams("countryCode",countryCode).
8282
when().
@@ -87,7 +87,7 @@ public void checkCountryInResponse_exceptIndia(String zipcode,String countryCode
8787
}
8888

8989
@Test (priority = 5, dependsOnMethods = "checkStatusCode_expectHttp200", dataProvider = "place")
90-
public void checkCountryAbbreviationInResponse_exceptFortMarket(String zipcode, String countryCode, String Place, String State, String Country){
90+
public void checkCountryAbbreviationInResponse_expectTrue(String zipcode, String countryCode, String Place, String State, String Country){
9191
given().pathParams("zipcode",zipcode).
9292
pathParams("countryCode",countryCode).
9393
when().
@@ -98,7 +98,7 @@ public void checkCountryAbbreviationInResponse_exceptFortMarket(String zipcode,
9898
}
9999

100100
@Test (priority = 6, dependsOnMethods = "checkStatusCode_expectHttp200", dataProvider = "place")
101-
public void checkIfPlaceAvailable_exceptTrue(String zipcode, String countryCode, String Place, String State, String Country){
101+
public void checkIfPlaceAvailable_expectTrue(String zipcode, String countryCode, String Place, String State, String Country){
102102
given().pathParams("zipcode",zipcode).
103103
pathParams("countryCode",countryCode).
104104
when().
@@ -108,7 +108,7 @@ public void checkIfPlaceAvailable_exceptTrue(String zipcode, String countryCode,
108108
}
109109

110110
@Test (priority = 7, dependsOnMethods = "checkStatusCode_expectHttp200", dataProvider = "place")
111-
public void checkIfPlaceNotAvailable_exceptTrue(String zipcode, String countryCode, String Place, String State, String Country){
111+
public void checkIfPlaceNotAvailable_expectTrue(String zipcode, String countryCode, String Place, String State, String Country){
112112
given().pathParams("zipcode",zipcode).
113113
pathParams("countryCode",countryCode).
114114
when().
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
import io.restassured.builder.*;
2+
import io.restassured.http.ContentType;
3+
import io.restassured.specification.RequestSpecification;
4+
import io.restassured.specification.ResponseSpecification;
5+
import org.testng.annotations.BeforeClass;
6+
import org.testng.annotations.BeforeTest;
7+
import org.testng.annotations.DataProvider;
8+
import org.testng.annotations.Test;
9+
10+
import static io.restassured.RestAssured.given;
11+
import static org.hamcrest.Matchers.*;
12+
import static org.hamcrest.Matchers.hasItem;
13+
14+
public class RequestResponseSpecBuilderTest {
15+
16+
private static RequestSpecification ReqSpec;
17+
private static ResponseSpecification ResSpec;
18+
19+
@BeforeTest
20+
public static void RequestSpecificBuilder(){
21+
ReqSpec = new RequestSpecBuilder().
22+
setBaseUri("https://zippopotam.us").
23+
setContentType(ContentType.JSON).
24+
build();
25+
}
26+
27+
@BeforeTest
28+
public static void ResponseSpecificBuilder(){
29+
ResSpec = new ResponseSpecBuilder().
30+
expectStatusCode(200).
31+
expectContentType(ContentType.JSON).
32+
build();
33+
}
34+
35+
@DataProvider(name="place")
36+
public Object[][] ZipCodePlaceDetails() {
37+
return new Object[][]{
38+
{"400001", "IN", "University Road", "Maharashtra", "India"},
39+
{"140001", "IN", "Rup Nagar College", "Punjab", "India"},
40+
{"110001", "IN", "Janpath", "New Delhi", "India"},
41+
{"400001", "IN", "Cannon Road", "Maharashtra", "India"},
42+
{"400001", "IN", "Fort Market", "Maharashtra", "India"},
43+
{"400001", "IN", "G T Hospital", "Maharashtra", "India"}
44+
};
45+
}
46+
47+
@BeforeClass
48+
public void logRequestAndResponseDetails(){
49+
given().
50+
spec(ReqSpec).
51+
log().all().
52+
when().
53+
get("IN/400001").
54+
then().
55+
log().body();
56+
57+
}
58+
59+
@Test(priority = 0, dataProvider = "place")
60+
public void checkStatusCodeAndContentType_expectHttp200andApplicationJSON(String zipcode, String countryCode, String Place, String State, String Country){
61+
given().
62+
spec(ReqSpec).
63+
pathParams("zipcode",zipcode).
64+
pathParams("countryCode",countryCode).
65+
when().
66+
get("{countryCode}/{zipcode}").
67+
then().
68+
spec(ResSpec);
69+
}
70+
71+
@Test (priority = 2, dependsOnMethods = "checkStatusCodeAndContentType_expectHttp200andApplicationJSON", dataProvider = "place")
72+
public void checkPostCodeInResponse_expectTrue(String zipcode, String countryCode, String Place, String State, String Country){
73+
given().
74+
spec(ReqSpec).
75+
pathParams("zipcode",zipcode).
76+
pathParams("countryCode",countryCode).
77+
when().
78+
get("/{countryCode}/{zipcode}").
79+
then().
80+
spec(ResSpec).
81+
and().
82+
assertThat().
83+
body("'post code'", equalTo(zipcode));
84+
}
85+
86+
@Test(priority = 3, dependsOnMethods = "checkStatusCodeAndContentType_expectHttp200andApplicationJSON", dataProvider = "place")
87+
public void checkStateNameInResponse_expectTrue(String zipcode, String countryCode, String Place, String State, String Country){
88+
given().spec(ReqSpec).
89+
pathParams("zipcode",zipcode).
90+
pathParams("countryCode",countryCode).
91+
when().
92+
get("/{countryCode}/{zipcode}").
93+
then().
94+
spec(ResSpec).
95+
and().
96+
assertThat().
97+
body("places[0].state", equalTo(State));
98+
}
99+
100+
@Test (priority = 4, dependsOnMethods = "checkStatusCodeAndContentType_expectHttp200andApplicationJSON", dataProvider = "place")
101+
public void checkCountryInResponse_expectTrue(String zipcode, String countryCode, String place, String State, String Country){
102+
given().spec(ReqSpec).
103+
pathParams("zipcode",zipcode).
104+
pathParams("countryCode",countryCode).
105+
when().
106+
get("/{countryCode}/{zipcode}").
107+
then().
108+
assertThat().
109+
body("country", equalTo(Country));
110+
}
111+
112+
@Test (priority = 5, dependsOnMethods = "checkStatusCodeAndContentType_expectHttp200andApplicationJSON", dataProvider = "place")
113+
public void checkCountryAbbreviationInResponse_expectTrue(String zipcode, String countryCode, String Place, String State, String Country){
114+
given().spec(ReqSpec).
115+
pathParams("zipcode",zipcode).
116+
pathParams("countryCode",countryCode).
117+
when().
118+
get("/{countryCode}/{zipcode}").
119+
then().
120+
assertThat().
121+
body("'country abbreviation'", equalTo(countryCode));
122+
}
123+
124+
@Test (priority = 6, dependsOnMethods = "checkStatusCodeAndContentType_expectHttp200andApplicationJSON", dataProvider = "place")
125+
public void checkIfPlaceAvailable_expectTrue(String zipcode, String countryCode, String Place, String State, String Country){
126+
given().spec(ReqSpec).
127+
pathParams("zipcode",zipcode).
128+
pathParams("countryCode",countryCode).
129+
when().
130+
get("/{countryCode}/{zipcode}").
131+
then().assertThat().
132+
body("places.'place name'", hasItem(Place));
133+
}
134+
135+
@Test (priority = 7, dependsOnMethods = "checkStatusCodeAndContentType_expectHttp200andApplicationJSON", dataProvider = "place")
136+
public void checkIfPlaceNotAvailable_expectTrue(String zipcode, String countryCode, String Place, String State, String Country){
137+
given().spec(ReqSpec).
138+
pathParams("zipcode",zipcode).
139+
pathParams("countryCode",countryCode).
140+
when().
141+
get("/{countryCode}/{zipcode}").
142+
then().spec(ResSpec).
143+
and().
144+
assertThat().
145+
body("places.'place name'", not(hasItem("Mumbai")));
146+
}
147+
148+
}

0 commit comments

Comments
 (0)