Skip to content

Commit 24712cc

Browse files
committed
creating tests using restAssured
1 parent f9b884d commit 24712cc

File tree

8 files changed

+330
-0
lines changed

8 files changed

+330
-0
lines changed

.gitignore

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
!**/src/main/**/target/
4+
!**/src/test/**/target/
5+
6+
### IntelliJ IDEA ###
7+
.idea/modules.xml
8+
.idea/jarRepositories.xml
9+
.idea/compiler.xml
10+
.idea/libraries/
11+
*.iws
12+
*.iml
13+
*.ipr
14+
15+
### Eclipse ###
16+
.apt_generated
17+
.classpath
18+
.factorypath
19+
.project
20+
.settings
21+
.springBeans
22+
.sts4-cache
23+
24+
### NetBeans ###
25+
/nbproject/private/
26+
/nbbuild/
27+
/dist/
28+
/nbdist/
29+
/.nb-gradle/
30+
build/
31+
!**/src/main/**/build/
32+
!**/src/test/**/build/
33+
34+
### VS Code ###
35+
.vscode/
36+
37+
### Mac OS ###
38+
.DS_Store

.idea/.gitignore

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

.idea/encodings.xml

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

.idea/misc.xml

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

pom.xml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.example</groupId>
8+
<artifactId>RestAssuredUsingTestNG</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>8</maven.compiler.source>
13+
<maven.compiler.target>8</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>io.rest-assured</groupId>
20+
<artifactId>rest-assured</artifactId>
21+
<version>5.1.1</version>
22+
<!-- <scope>test</scope> -->
23+
</dependency>
24+
25+
<dependency>
26+
<groupId>org.testng</groupId>
27+
<artifactId>testng</artifactId>
28+
<version>7.7.0</version>
29+
<scope>test</scope>
30+
</dependency>
31+
</dependencies>
32+
33+
</project>

src/test/java/BasicAPITest.java

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import io.restassured.http.ContentType;
2+
import org.testng.annotations.Test;
3+
import static io.restassured.RestAssured.given;
4+
import static org.hamcrest.Matchers.equalTo;
5+
import static org.hamcrest.Matchers.*;
6+
7+
public class BasicAPITest {
8+
9+
@Test (priority = 0)
10+
public void checkStatusCode_expectHttp200(){
11+
given().
12+
when().
13+
get("http://zippopotam.us/IN/400001").
14+
then().assertThat().statusCode(200);
15+
}
16+
@Test (priority = 1, dependsOnMethods = "checkStatusCode_expectHttp200")
17+
public void checkContentType_expectApplicationJson(){
18+
given().
19+
when().
20+
get("http://zippopotam.us/IN/400001").
21+
then().
22+
assertThat().
23+
contentType(ContentType.JSON);
24+
}
25+
26+
@Test (priority = 2, dependsOnMethods = "checkStatusCode_expectHttp200")
27+
public void checkPlaceNameInResponse_exceptFortMarket(){
28+
given().
29+
when().
30+
get("http://zippopotam.us/IN/400001").
31+
then().
32+
assertThat().
33+
body("places[8].'place name'", equalTo("Fort Market"));
34+
}
35+
@Test (priority = 3, dependsOnMethods = "checkStatusCode_expectHttp200")
36+
public void checkStateNameInResponse_exceptMaharashtra(){
37+
given().
38+
when().
39+
get("http://zippopotam.us/IN/400001").
40+
then().
41+
assertThat().
42+
body("places[8].state", equalTo("Maharashtra"));
43+
}
44+
@Test (priority = 4, dependsOnMethods = "checkStatusCode_expectHttp200")
45+
public void checkCountryInResponse_exceptIndia(){
46+
given().
47+
when().
48+
get("http://zippopotam.us/IN/400001").
49+
then().
50+
assertThat().
51+
body("country", equalTo("India"));
52+
}
53+
@Test (priority = 5, dependsOnMethods = "checkStatusCode_expectHttp200")
54+
public void checkCountryAbbreviationInResponse_exceptFortMarket(){
55+
given().
56+
when().
57+
get("http://zippopotam.us/IN/400001").
58+
then().
59+
assertThat().
60+
body("'country abbreviation'", equalTo("IN"));
61+
}
62+
@Test (priority = 6, dependsOnMethods = "checkStatusCode_expectHttp200")
63+
public void checkIfPlaceAvailable_exceptTrue(){
64+
given().
65+
when().
66+
get("http://zippopotam.us/IN/400001").
67+
then().assertThat().
68+
body("places.'place name'", hasItem("Fort Street"));
69+
}
70+
@Test (priority = 7, dependsOnMethods = "checkStatusCode_expectHttp200")
71+
public void checkIfPlaceNotAvailable_exceptTrue(){
72+
given().
73+
when().
74+
get("http://zippopotam.us/IN/400001").
75+
then().assertThat().
76+
body("places.'place name'", not(hasItem("Mumbai")));
77+
}
78+
@Test (priority = 8, dependsOnMethods = "checkStatusCode_expectHttp200")
79+
public void checkNumberOfPlaceAvailable_except26(){
80+
given().
81+
when().
82+
get("http://zippopotam.us/IN/400001").
83+
then().assertThat().
84+
body("places.'place name'", hasSize(26));
85+
}
86+
}
87+

src/test/java/FirstTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import org.testng.annotations.Test;
2+
import static io.restassured.RestAssured.*;
3+
4+
public class FirstTest {
5+
6+
@Test
7+
public void checkStatusCode_expect200(){
8+
9+
given().
10+
when().
11+
get("http://zippopotam.us/IN/400001").
12+
then().
13+
assertThat().statusCode(200);
14+
15+
}
16+
@Test
17+
public void logRequestAndResponseDetails(){
18+
given().
19+
log().all().
20+
when().
21+
get("http://zippopotam.us/IN/400001").
22+
then().
23+
log().body();
24+
25+
}
26+
27+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import io.restassured.http.ContentType;
2+
import org.testng.annotations.BeforeTest;
3+
import org.testng.annotations.DataProvider;
4+
import org.testng.annotations.Test;
5+
import static io.restassured.RestAssured.given;
6+
import static org.hamcrest.Matchers.equalTo;
7+
import static org.hamcrest.Matchers.*;
8+
9+
public class ParameterizingBasicAPITest {
10+
11+
@DataProvider (name="place")
12+
public Object[][] ZipCodePlaceDetails(){
13+
return new Object[][]{
14+
{"400001", "IN", "University Road", "Maharashtra", "India"},
15+
{"140001", "IN", "Rup Nagar College", "Punjab", "India"},
16+
{"110001", "IN","Janpath", "New Delhi", "India"},
17+
{"400001", "IN", "Cannon Road", "Maharashtra", "India"},
18+
{"400001", "IN", "Fort Market", "Maharashtra", "India"},
19+
{"400001", "IN", "G T Hospital", "Maharashtra", "India"}
20+
21+
};
22+
}
23+
24+
@BeforeTest
25+
public void logRequestAndResponseDetails(){
26+
given().
27+
log().all().
28+
when().
29+
get("http://zippopotam.us/IN/400001").
30+
then().
31+
log().body();
32+
33+
}
34+
35+
@Test (priority = 0, dataProvider = "place")
36+
public void checkStatusCode_expectHttp200(String zipcode, String countryCode, String Place, String State, String Country){
37+
given().
38+
pathParams("zipcode",zipcode).
39+
pathParams("countryCode",countryCode).
40+
when().
41+
get("http://zippopotam.us/{countryCode}/{zipcode}").
42+
then().assertThat().statusCode(200);
43+
}
44+
45+
@Test (priority = 1, dependsOnMethods = "checkStatusCode_expectHttp200", dataProvider = "place")
46+
public void checkContentType_expectApplicationJson(String zipcode, String countryCode, String Place, String State, String Country){
47+
given().pathParams("zipcode",zipcode).
48+
pathParams("countryCode",countryCode).
49+
when().
50+
get("http://zippopotam.us/{countryCode}/{zipcode}").
51+
then().
52+
assertThat().
53+
contentType(ContentType.JSON);
54+
}
55+
56+
@Test (priority = 2, dependsOnMethods = "checkStatusCode_expectHttp200", dataProvider = "place")
57+
public void checkPostCodeInResponse_exceptFortMarket(String zipcode, String countryCode, String Place, String State, String Country){
58+
given().pathParams("zipcode",zipcode).
59+
pathParams("countryCode",countryCode).
60+
when().
61+
get("http://zippopotam.us/{countryCode}/{zipcode}").
62+
then().
63+
assertThat().
64+
body("'post code'", equalTo(zipcode));
65+
}
66+
67+
@Test(priority = 3, dependsOnMethods = "checkStatusCode_expectHttp200", dataProvider = "place")
68+
public void checkStateNameInResponse_exceptMaharashtra(String zipcode, String countryCode, String Place, String State, String Country){
69+
given().pathParams("zipcode",zipcode).
70+
pathParams("countryCode",countryCode).
71+
when().
72+
get("http://zippopotam.us/{countryCode}/{zipcode}").
73+
then().
74+
assertThat().
75+
body("places[0].state", equalTo(State));
76+
}
77+
78+
@Test (priority = 4, dependsOnMethods = "checkStatusCode_expectHttp200", dataProvider = "place")
79+
public void checkCountryInResponse_exceptIndia(String zipcode,String countryCode, String place, String State, String Country){
80+
given().pathParams("zipcode",zipcode).
81+
pathParams("countryCode",countryCode).
82+
when().
83+
get("http://zippopotam.us/{countryCode}/{zipcode}").
84+
then().
85+
assertThat().
86+
body("country", equalTo(Country));
87+
}
88+
89+
@Test (priority = 5, dependsOnMethods = "checkStatusCode_expectHttp200", dataProvider = "place")
90+
public void checkCountryAbbreviationInResponse_exceptFortMarket(String zipcode, String countryCode, String Place, String State, String Country){
91+
given().pathParams("zipcode",zipcode).
92+
pathParams("countryCode",countryCode).
93+
when().
94+
get("http://zippopotam.us/{countryCode}/{zipcode}").
95+
then().
96+
assertThat().
97+
body("'country abbreviation'", equalTo(countryCode));
98+
}
99+
100+
@Test (priority = 6, dependsOnMethods = "checkStatusCode_expectHttp200", dataProvider = "place")
101+
public void checkIfPlaceAvailable_exceptTrue(String zipcode, String countryCode, String Place, String State, String Country){
102+
given().pathParams("zipcode",zipcode).
103+
pathParams("countryCode",countryCode).
104+
when().
105+
get("http://zippopotam.us/{countryCode}/{zipcode}").
106+
then().assertThat().
107+
body("places.'place name'", hasItem(Place));
108+
}
109+
110+
@Test (priority = 7, dependsOnMethods = "checkStatusCode_expectHttp200", dataProvider = "place")
111+
public void checkIfPlaceNotAvailable_exceptTrue(String zipcode, String countryCode, String Place, String State, String Country){
112+
given().pathParams("zipcode",zipcode).
113+
pathParams("countryCode",countryCode).
114+
when().
115+
get("http://zippopotam.us/{countryCode}/{zipcode}").
116+
then().assertThat().
117+
body("places.'place name'", not(hasItem("Mumbai")));
118+
}
119+
120+
}
121+

0 commit comments

Comments
 (0)