Skip to content

Commit 6441b06

Browse files
New tests added for response verification blog on Medium (#136)
* added test to perform inline response verification * added test to perform response verification using pojo * added json file verification test
1 parent 29c8b82 commit 6441b06

File tree

5 files changed

+250
-0
lines changed

5 files changed

+250
-0
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<hamcrest.version>3.0</hamcrest.version>
2323
<json-simple.version>1.1.1</json-simple.version>
2424
<org-json.verion>20250517</org-json.verion>
25+
<jsonassert.version>1.5.3</jsonassert.version>
2526
<allure.version>2.31.0</allure.version>
2627
<awaitility.version>4.3.0</awaitility.version>
2728
<commons-lang3.version>3.20.0</commons-lang3.version>
@@ -113,6 +114,11 @@
113114
<artifactId>gson</artifactId>
114115
<version>${google-gson.verion}</version>
115116
</dependency>
117+
<dependency>
118+
<groupId>org.skyscreamer</groupId>
119+
<artifactId>jsonassert</artifactId>
120+
<version>${jsonassert.version}</version>
121+
</dependency>
116122
</dependencies>
117123
<build>
118124
<pluginManagement>
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
package restfulecommerce.tutorial;
2+
3+
import static io.restassured.RestAssured.given;
4+
import static org.hamcrest.core.IsEqual.equalTo;
5+
import static org.testng.Assert.assertEquals;
6+
7+
import java.io.IOException;
8+
import java.nio.file.Files;
9+
import java.nio.file.Paths;
10+
import java.util.Objects;
11+
12+
import com.google.gson.Gson;
13+
import com.google.gson.JsonArray;
14+
import com.google.gson.JsonObject;
15+
import io.restassured.path.json.JsonPath;
16+
import io.restassured.response.Response;
17+
import org.skyscreamer.jsonassert.JSONAssert;
18+
import org.testng.annotations.Test;
19+
import restfulecommerce.tutorial.pojo.OrderResponse;
20+
21+
public class ResponseBodyVerificationTest {
22+
23+
@Test
24+
public void testResponseWithMatchers () {
25+
26+
given ().when ()
27+
.queryParam ("id", 1)
28+
.log ()
29+
.all ()
30+
.get ("http://localhost:3004/getOrder")
31+
.then ()
32+
.log ()
33+
.all ()
34+
.statusCode (200)
35+
.and ()
36+
.assertThat ()
37+
.body ("message", equalTo ("Order found!!"))
38+
.body ("orders[0].id", equalTo (1));
39+
}
40+
41+
@Test
42+
public void testExtractResponseAndVerify () {
43+
Response response = given ().when ()
44+
.log ()
45+
.all ()
46+
.get ("http://localhost:3004/getAllOrders")
47+
.then ()
48+
.log ()
49+
.all ()
50+
.statusCode (200)
51+
.extract ()
52+
.response ();
53+
54+
String orderMessage = response.jsonPath ()
55+
.getString ("message");
56+
assertEquals (orderMessage, "Orders fetched successfully!");
57+
String orderTwoUserId = response.jsonPath ()
58+
.getString ("orders[1].user_id");
59+
assertEquals (orderTwoUserId, "484");
60+
61+
JsonPath jsonPath = new JsonPath (response.asString ());
62+
assertEquals (jsonPath.getString ("orders[0].product_name"), "Small Marble Shoes");
63+
}
64+
65+
@Test
66+
public void testExtractResponseWithGson () {
67+
String response = given ().when ()
68+
.log ()
69+
.all ()
70+
.get ("http://localhost:3004/getAllOrders")
71+
.then ()
72+
.log ()
73+
.all ()
74+
.statusCode (200)
75+
.extract ()
76+
.response ()
77+
.asString ();
78+
79+
Gson gson = new Gson ();
80+
JsonObject responseObject = gson.fromJson (response, JsonObject.class);
81+
String orderText = responseObject.get ("message")
82+
.getAsString ();
83+
assertEquals (orderText, "Orders fetched successfully!");
84+
85+
JsonArray orderArray = responseObject.getAsJsonArray ("orders");
86+
String orderThreeProductName = orderArray.get (2)
87+
.getAsJsonObject ()
88+
.get ("product_name")
89+
.getAsString ();
90+
assertEquals (orderThreeProductName, "Mediocre Aluminum Coat");
91+
}
92+
93+
@Test
94+
public void testMultipleResponseConditions () {
95+
given ().when ()
96+
.log ()
97+
.all ()
98+
.get ("http://localhost:3004/getAllOrders")
99+
.then ()
100+
.log ()
101+
.all ()
102+
.statusCode (200)
103+
.and ()
104+
.assertThat ()
105+
.body ("message", equalTo ("Orders fetched successfully!"), "orders[0].user_id", equalTo ("466"),
106+
"orders[0].product_id", equalTo ("357"), "orders[0].product_name", equalTo ("Small Marble Shoes"),
107+
"orders[0].product_amount", equalTo (1558));
108+
}
109+
110+
@Test
111+
public void testDeserializeResponseWithPojo () {
112+
OrderResponse orderResponse = given ().when ()
113+
.log ()
114+
.all ()
115+
.get ("http://localhost:3004/getAllOrders")
116+
.then ()
117+
.log ()
118+
.all ()
119+
.statusCode (200)
120+
.extract ()
121+
.body ()
122+
.as (OrderResponse.class);
123+
124+
assertEquals (orderResponse.getMessage (), "Orders fetched successfully!");
125+
assertEquals (orderResponse.getOrders ()
126+
.get (0)
127+
.getId (), 1);
128+
assertEquals (orderResponse.getOrders ()
129+
.get (1)
130+
.getTaxAmt (), 1228);
131+
132+
}
133+
134+
@Test
135+
public void testResponseFromJsonFile () {
136+
String actualResponse = given ().when ()
137+
.log ()
138+
.all ()
139+
.get ("http://localhost:3004/getAllOrders")
140+
.then ()
141+
.log ()
142+
.all ()
143+
.statusCode (200)
144+
.extract ()
145+
.response ()
146+
.asString ();
147+
148+
String expectedJson = null;
149+
try {
150+
expectedJson = Files.readString (Paths.get (Objects.requireNonNull (this.getClass ()
151+
.getClassLoader ()
152+
.getResource ("AllOrders.json"))
153+
.getPath ()));
154+
} catch (IOException e) {
155+
throw new RuntimeException (e);
156+
}
157+
158+
JSONAssert.assertEquals (expectedJson, actualResponse, true);
159+
160+
}
161+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package restfulecommerce.tutorial.pojo;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import lombok.Getter;
5+
import lombok.Setter;
6+
7+
@Getter
8+
@Setter
9+
public class Order {
10+
private int id;
11+
@JsonProperty ("user_id")
12+
private String userId;
13+
@JsonProperty ("product_id")
14+
private String productId;
15+
@JsonProperty ("product_name")
16+
private String productName;
17+
@JsonProperty ("product_amount")
18+
private int productAmount;
19+
private int qty;
20+
@JsonProperty ("tax_amt")
21+
private int taxAmt;
22+
@JsonProperty ("total_amt")
23+
private int totalAmt;
24+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package restfulecommerce.tutorial.pojo;
2+
3+
import java.util.List;
4+
5+
import lombok.Getter;
6+
import lombok.Setter;
7+
8+
@Getter
9+
@Setter
10+
public class OrderResponse {
11+
private String message;
12+
private List<Order> orders;
13+
14+
}

src/test/resources/AllOrders.json

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"message": "Orders fetched successfully!",
3+
"orders": [
4+
{
5+
"id": 1,
6+
"user_id": "466",
7+
"product_id": "357",
8+
"product_name": "Small Marble Shoes",
9+
"product_amount": 1558,
10+
"qty": 9,
11+
"tax_amt": 1402,
12+
"total_amt": 15424
13+
},
14+
{
15+
"id": 2,
16+
"user_id": "484",
17+
"product_id": "219",
18+
"product_name": "Aerodynamic Bronze Chair",
19+
"product_amount": 1755,
20+
"qty": 7,
21+
"tax_amt": 1228,
22+
"total_amt": 13513
23+
},
24+
{
25+
"id": 3,
26+
"user_id": "386",
27+
"product_id": "342",
28+
"product_name": "Mediocre Aluminum Coat",
29+
"product_amount": 335,
30+
"qty": 1,
31+
"tax_amt": 33,
32+
"total_amt": 368
33+
},
34+
{
35+
"id": 4,
36+
"user_id": "481",
37+
"product_id": "220",
38+
"product_name": "Fantastic Concrete Table",
39+
"product_amount": 79,
40+
"qty": 1,
41+
"tax_amt": 7,
42+
"total_amt": 86
43+
}
44+
]
45+
}

0 commit comments

Comments
 (0)