Skip to content

Commit 95f97ee

Browse files
committed
test(cucumber_get_post): use datatable instead of chaining And
1 parent 02d50f5 commit 95f97ee

3 files changed

Lines changed: 15 additions & 12 deletions

File tree

src/test/java/feature/StepDefinition.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
import com.fasterxml.jackson.databind.JsonNode;
44
import com.fasterxml.jackson.databind.ObjectMapper;
55
import com.xpeho.spring_boot_java_random_user.domain.entities.UserRequest;
6+
import io.cucumber.datatable.DataTable;
67
import io.cucumber.java.en.And;
78
import io.cucumber.java.en.Given;
89
import io.cucumber.java.en.Then;
910
import io.cucumber.java.en.When;
1011

12+
import java.util.List;
13+
1114
import static org.junit.jupiter.api.Assertions.assertEquals;
1215

1316
public class StepDefinition extends SpringIntegrationTest {
@@ -39,16 +42,13 @@ public void theResponseStatusShouldBe(int expectedStatus) {
3942
assertEquals(expectedStatus, latestResponse.getStatusCode().value());
4043
}
4144

42-
@And("the response should contain the created user id {long}")
43-
public void theResponseShouldContainTheCreatedUserId(long expectedId) throws Exception {
44-
JsonNode body = objectMapper.readTree(latestResponse.getBody());
45-
assertEquals(expectedId, body.get("id").asLong());
46-
}
47-
48-
@And("the response should contain the firstname {string}")
49-
public void theResponseShouldContainTheFirstname(String expectedFirstname) throws Exception {
45+
@And("the user profile")
46+
public void theUserProfile(DataTable table) throws Exception{
5047
JsonNode body = objectMapper.readTree(latestResponse.getBody());
51-
assertEquals(expectedFirstname, body.get("firstname").asText());
48+
List<List<String>> rows = table.asLists(String.class);
49+
for (List<String> row : rows) {
50+
assertEquals(row.get(1), body.get(row.get(0)).asText());
51+
}
5252
}
5353

5454
@When("the client call to GET \\/random-users\\/{int}")

src/test/resources/features/create_user.feature

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ Feature: Create user endpoint
44
Given a valid user payload for creation
55
When the client call to POST /random-users
66
Then the response status should be 201
7-
And the response should contain the created user id 1
8-
And the response should contain the firstname "Emma"
7+
And the user profile
8+
| id | 1 |
9+
| firstname | Emma |

src/test/resources/features/get_user.feature

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ Feature: Get user endpoint
66
Then the response status should be 201
77
When the client call to GET /random-users/1
88
Then the response status should be 200
9-
And the response should contain the firstname "Emma"
9+
And the user profile
10+
| id | 1 |
11+
| firstname | Emma |
1012

1113
Scenario: Get a user that does not exist
1214
When the client call to GET /random-users/999

0 commit comments

Comments
 (0)