Skip to content

Commit 5f0bd9e

Browse files
committed
test(cucumber_get_post): add one integration test for post
1 parent 01fd3c2 commit 5f0bd9e

7 files changed

Lines changed: 170 additions & 11 deletions

File tree

pom.xml

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,21 @@
8383
<artifactId>spring-boot-starter-data-jdbc-test</artifactId>
8484
<scope>test</scope>
8585
</dependency>
86-
<dependency>
87-
<groupId>org.springframework.boot</groupId>
88-
<artifactId>spring-boot-starter-webmvc-test</artifactId>
89-
<scope>test</scope>
90-
</dependency>
91-
<dependency>
92-
<groupId>org.springframework.boot</groupId>
93-
<artifactId>spring-boot-testcontainers</artifactId>
94-
<scope>test</scope>
95-
</dependency>
86+
<dependency>
87+
<groupId>org.springframework.boot</groupId>
88+
<artifactId>spring-boot-starter-webmvc-test</artifactId>
89+
<scope>test</scope>
90+
</dependency>
91+
<dependency>
92+
<groupId>org.springframework.boot</groupId>
93+
<artifactId>spring-boot-restclient</artifactId>
94+
<scope>test</scope>
95+
</dependency>
96+
<dependency>
97+
<groupId>org.springframework.boot</groupId>
98+
<artifactId>spring-boot-testcontainers</artifactId>
99+
<scope>test</scope>
100+
</dependency>
96101
<dependency>
97102
<groupId>org.testcontainers</groupId>
98103
<artifactId>testcontainers-junit-jupiter</artifactId>
@@ -111,7 +116,7 @@
111116
</dependency>
112117
<dependency>
113118
<groupId>io.cucumber</groupId>
114-
<artifactId>cucumber-junit</artifactId>
119+
<artifactId>cucumber-junit-platform-engine</artifactId>
115120
<version>7.14.0</version>
116121
<scope>test</scope>
117122
</dependency>
@@ -121,6 +126,17 @@
121126
<version>7.14.0</version>
122127
<scope>test</scope>
123128
</dependency>
129+
<dependency>
130+
<groupId>org.junit.platform</groupId>
131+
<artifactId>junit-platform-suite</artifactId>
132+
<scope>test</scope>
133+
</dependency>
134+
<dependency>
135+
<groupId>org.springframework.boot</groupId>
136+
<artifactId>spring-boot-starter-test</artifactId>
137+
<scope>test</scope>
138+
</dependency>
139+
124140
</dependencies>
125141

126142
<build>

src/main/java/com/xpeho/spring_boot_java_random_user/data/models/db/User.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,21 @@
88
public class User {
99
@Id
1010
private Long id;
11+
@Column("gender")
1112
private String gender;
1213
@Column("firstname")
1314
private String firstname;
1415
@Column("lastname")
1516
private String lastname;
17+
@Column("civility")
1618
private String civility;
19+
@Column("email")
1720
private String email;
21+
@Column("phone")
1822
private String phone;
23+
@Column("picture")
1924
private String picture;
25+
@Column("nationality")
2026
private String nationality;
2127

2228
// Required by Spring Data JDBC to instantiate the entity via reflection
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package feature;
2+
3+
import org.junit.platform.suite.api.ConfigurationParameter;
4+
import org.junit.platform.suite.api.IncludeEngines;
5+
import org.junit.platform.suite.api.SelectClasspathResource;
6+
import org.junit.platform.suite.api.Suite;
7+
8+
import static io.cucumber.junit.platform.engine.Constants.GLUE_PROPERTY_NAME;
9+
import static io.cucumber.junit.platform.engine.Constants.PLUGIN_PROPERTY_NAME;
10+
11+
@Suite
12+
@IncludeEngines("cucumber")
13+
@SelectClasspathResource("features")
14+
@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "feature")
15+
@ConfigurationParameter(key = PLUGIN_PROPERTY_NAME, value = "pretty")
16+
public class CucumberIntegrationTest {
17+
18+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package feature;
2+
3+
import com.xpeho.spring_boot_java_random_user.SpringBootJavaRandomUserApplication;
4+
import io.cucumber.spring.CucumberContextConfiguration;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.resttestclient.TestRestTemplate;
7+
import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureTestRestTemplate;
8+
import org.springframework.boot.test.context.SpringBootTest;
9+
import org.springframework.boot.test.web.server.LocalServerPort;
10+
import org.springframework.http.HttpEntity;
11+
import org.springframework.http.HttpHeaders;
12+
import org.springframework.http.MediaType;
13+
import org.springframework.http.ResponseEntity;
14+
import org.springframework.test.context.ActiveProfiles;
15+
16+
@CucumberContextConfiguration
17+
@ActiveProfiles("test")
18+
@AutoConfigureTestRestTemplate
19+
@SpringBootTest(
20+
classes = SpringBootJavaRandomUserApplication.class,
21+
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
22+
properties = {
23+
"spring.sql.init.mode=always",
24+
"spring.sql.init.schema-locations=classpath:schema-h2.sql"
25+
}
26+
)
27+
public class SpringIntegrationTest {
28+
29+
@Autowired
30+
protected TestRestTemplate restTemplate;
31+
32+
@LocalServerPort
33+
protected int port;
34+
35+
protected ResponseEntity<String> latestResponse;
36+
37+
protected void executePost(String path, Object payload) {
38+
String url = "http://localhost:" + port + path;
39+
HttpHeaders headers = new HttpHeaders();
40+
headers.setContentType(MediaType.APPLICATION_JSON);
41+
HttpEntity<Object> request = new HttpEntity<>(payload, headers);
42+
latestResponse = restTemplate.postForEntity(url, request, String.class);
43+
}
44+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package feature;
2+
3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.xpeho.spring_boot_java_random_user.domain.entities.UserRequest;
6+
import io.cucumber.java.en.And;
7+
import io.cucumber.java.en.Given;
8+
import io.cucumber.java.en.Then;
9+
import io.cucumber.java.en.When;
10+
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
13+
public class StepDefinition extends SpringIntegrationTest {
14+
15+
private final ObjectMapper objectMapper = new ObjectMapper();
16+
private UserRequest payload;
17+
18+
@Given("a valid user payload for creation")
19+
public void aValidUserPayloadForCreation() {
20+
payload = new UserRequest(
21+
"female",
22+
"Emma",
23+
"Stone",
24+
"Ms",
25+
"emma@example.com",
26+
"0644444444",
27+
"emma.jpg",
28+
"FR"
29+
);
30+
}
31+
32+
@When("the client call to POST \\/random-users")
33+
public void theClientCallToPostRandomUser() {
34+
executePost("/random-users", payload);
35+
}
36+
37+
@Then("the response status should be {int}")
38+
public void theResponseStatusShouldBe(int expectedStatus) {
39+
assertEquals(expectedStatus, latestResponse.getStatusCode().value());
40+
}
41+
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 {
50+
JsonNode body = objectMapper.readTree(latestResponse.getBody());
51+
assertEquals(expectedFirstname, body.get("firstname").asText());
52+
}
53+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Feature: Create user endpoint
2+
3+
Scenario: Create a user successfully
4+
Given a valid user payload for creation
5+
When the client call to POST /random-users
6+
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"

src/test/resources/schema-h2.sql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
DROP TABLE IF EXISTS "users";
2+
3+
CREATE TABLE IF NOT EXISTS "users"
4+
(
5+
"id" BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
6+
"gender" VARCHAR(20),
7+
"firstname" VARCHAR(100),
8+
"lastname" VARCHAR(100),
9+
"civility" VARCHAR(20),
10+
"email" VARCHAR(255),
11+
"phone" VARCHAR(50),
12+
"picture" VARCHAR(500),
13+
"nationality" VARCHAR(10)
14+
);

0 commit comments

Comments
 (0)