Skip to content

Commit 7662fa6

Browse files
committed
More single post tests
1 parent 67583c7 commit 7662fa6

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

src/test/java/com/typicode/jsonplaceholder/features/Posts.feature

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,29 @@ Feature: Posts Endpoint
1313
And the response body follows the "GetSinglePost" JSON schema
1414
And the response body matches the 1st post in the "GetAllPosts" expected response
1515

16+
Scenario: Verify single post using datatable
17+
When I make a GET request to the Posts endpoint with a path parameter of 1
18+
Then the response has a status code of 200
19+
And the response body follows the "GetSinglePost" JSON schema
20+
And the response body matches the following
21+
| key | value |
22+
| id | 1 |
23+
| title | sunt aut facere repellat provident occaecati excepturi optio reprehenderit |
24+
| body | quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto |
25+
| userId | 1 |
26+
27+
Scenario: Verify single post field by field
28+
When I make a GET request to the Posts endpoint with a path parameter of 1
29+
Then the response has a status code of 200
30+
And the response body follows the "GetSinglePost" JSON schema
31+
And the "id" field in the response body has a value of 1
32+
And the "title" field in the response body has a value of "sunt aut facere repellat provident occaecati excepturi optio reprehenderit"
33+
And the "body" field in the response body has a value of
34+
"""
35+
quia et suscipit
36+
suscipit recusandae consequuntur expedita et cum
37+
reprehenderit molestiae ut ut quas totam
38+
nostrum rerum est autem sunt rem eveniet architecto
39+
"""
40+
And the "userId" field in the response body has a value of 1
41+

src/test/java/com/typicode/jsonplaceholder/steps/CommonSteps.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,5 +102,13 @@ public static void verifyResponseBodyAgainstPartOfExpectedResponse(int index, St
102102
assertEquals(expected.toString(), actual.toString());
103103
}
104104

105+
@Then("the response body matches the following")
106+
public static void verifyResponseBodyAgainstDataTable(DataTable dataTable) {
107+
Map<String, String> expectedBody = dataTable.subTable(1, 0).asMap(String.class, String.class);
108+
JSONObject actual = new JSONObject(response.body());
109+
assertEquals(expectedBody.keySet(), actual.keySet());
110+
expectedBody.forEach((k,v) -> assertEquals(expectedBody.get(k), actual.get(k).toString()));
111+
}
112+
105113

106114
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.typicode.jsonplaceholder.steps;
2+
3+
import io.cucumber.java.en.Then;
4+
import org.json.JSONObject;
5+
6+
import static com.typicode.jsonplaceholder.steps.CommonSteps.response;
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class PostsSteps {
10+
11+
@Then("^the \"(id|userId)\" field in the response body has a value of (\\d+)$")
12+
public static void verifyResponseFieldValue(String field, int value) {
13+
JSONObject actual = new JSONObject(response.body());
14+
assertEquals(value, actual.get(field));
15+
}
16+
17+
@Then("the {string} field in the response body has a value of {string}")
18+
public static void verifyResponseFieldValue(String field, String value) {
19+
JSONObject actual = new JSONObject(response.body());
20+
assertEquals(value, actual.get(field));
21+
}
22+
23+
@Then("the {string} field in the response body has a value of")
24+
public static void verifyResponseFieldDocstring(String field, String value) {
25+
JSONObject actual = new JSONObject(response.body());
26+
assertEquals(value, actual.get(field));
27+
}
28+
}

0 commit comments

Comments
 (0)