Skip to content

Commit 1dcf33f

Browse files
shryhustboychuk
authored andcommitted
GP-83 update pom and tests
1 parent ec77c48 commit 1dcf33f

File tree

3 files changed

+159
-49
lines changed

3 files changed

+159
-49
lines changed

3-0-spring-framework/3-2-1-account-rest-api/pom.xml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,25 @@
1616
<dependency>
1717
<groupId>org.springframework</groupId>
1818
<artifactId>spring-webmvc</artifactId>
19-
<version>5.0.7.RELEASE</version>
19+
<version>5.2.12.RELEASE</version>
2020
</dependency>
2121
<dependency>
2222
<groupId>javax.servlet</groupId>
2323
<artifactId>javax.servlet-api</artifactId>
2424
<version>4.0.1</version>
2525
<scope>provided</scope>
2626
</dependency>
27+
<dependency>
28+
<groupId>org.springframework</groupId>
29+
<artifactId>spring-test</artifactId>
30+
<version>5.2.12.RELEASE</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.hamcrest</groupId>
34+
<artifactId>hamcrest-all</artifactId>
35+
<version>1.3</version>
36+
<scope>test</scope>
37+
</dependency>
2738
<dependency>
2839
<groupId>com.bobocode</groupId>
2940
<artifactId>spring-framework-exercises-util</artifactId>

3-0-spring-framework/3-2-1-account-rest-api/src/test/java/com/bobocode/AccountRestControllerTest.java

Lines changed: 71 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
import com.bobocode.config.RootConfig;
44
import com.bobocode.config.WebConfig;
5+
import com.bobocode.dao.AccountDao;
56
import com.bobocode.dao.impl.InMemoryAccountDao;
67
import com.bobocode.model.Account;
78
import com.bobocode.web.controller.AccountRestController;
8-
import org.junit.jupiter.api.BeforeEach;
9-
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.api.*;
1010
import org.springframework.beans.factory.annotation.Autowired;
1111
import org.springframework.http.MediaType;
1212
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;
@@ -16,19 +16,17 @@
1616
import org.springframework.web.bind.annotation.RestController;
1717
import org.springframework.web.context.WebApplicationContext;
1818

19-
import static org.hamcrest.MatcherAssert.assertThat;
20-
import static org.hamcrest.Matchers.arrayContaining;
21-
import static org.hamcrest.Matchers.arrayWithSize;
19+
import java.lang.reflect.Constructor;
20+
21+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
2222
import static org.hamcrest.Matchers.hasItems;
23-
import static org.hamcrest.core.IsNull.notNullValue;
24-
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
25-
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
26-
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
27-
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
23+
import static org.junit.jupiter.api.Assertions.assertNotNull;
24+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
2825
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
2926
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
3027

3128
@SpringJUnitWebConfig(classes = {RootConfig.class, WebConfig.class})
29+
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
3230
class AccountRestControllerTest {
3331
@Autowired
3432
private WebApplicationContext applicationContext;
@@ -45,47 +43,38 @@ void setup() {
4543
}
4644

4745
@Test
48-
void testAccountRestControllerAnnotation() {
46+
@Order(1)
47+
@DisplayName("AccountRestController is marked as @RestController")
48+
void accountRestControllerAnnotation() {
4949
RestController restController = AccountRestController.class.getAnnotation(RestController.class);
5050

51-
assertThat(restController, notNullValue());
51+
assertNotNull(restController);
5252
}
5353

5454
@Test
55-
void testAccountRestControllerRequestMapping() {
55+
@Order(2)
56+
@DisplayName("AccountRestController is annotated and mapped with @RequestMapping")
57+
void accountRestControllerRequestMapping() {
5658
RequestMapping requestMapping = AccountRestController.class.getAnnotation(RequestMapping.class);
5759

58-
assertThat(requestMapping, notNullValue());
59-
assertThat(requestMapping.value(), arrayWithSize(1));
60-
assertThat(requestMapping.value(), arrayContaining("/accounts"));
60+
assertNotNull(requestMapping);
61+
assertThat(requestMapping.value().length).isEqualTo(1);
62+
assertThat(requestMapping.value()).contains("/accounts");
6163
}
6264

6365
@Test
64-
void testHttpStatusCodeOnCreate() throws Exception {
65-
mockMvc.perform(
66-
post("/accounts")
67-
.contentType(MediaType.APPLICATION_JSON)
68-
.content("{\"firstName\":\"Johnny\", \"lastName\":\"Boy\", \"email\":\"jboy@gmail.com\"}"))
69-
.andExpect(status().isCreated());
70-
}
71-
72-
@Test
73-
void testCreateAccountReturnsAssignedId() throws Exception {
74-
mockMvc.perform(
75-
post("/accounts")
76-
.contentType(MediaType.APPLICATION_JSON)
77-
.content("{\"firstName\":\"Johnny\", \"lastName\":\"Boy\", \"email\":\"jboy@gmail.com\"}"))
78-
.andExpect(jsonPath("$.id").value(1L));
79-
}
66+
@Order(3)
67+
@DisplayName("AccountDao is injected using constructor")
68+
void accountDaoInjection() throws NoSuchMethodException {
69+
Constructor<AccountRestController> constructor = AccountRestController.class.getConstructor();
8070

81-
@Test
82-
void testGetAccountsResponseStatusCode() throws Exception {
83-
mockMvc.perform(get("/accounts").accept(MediaType.APPLICATION_JSON_UTF8))
84-
.andExpect(status().isOk());
71+
assertThat(constructor.getParameterTypes()).contains(AccountDao.class);
8572
}
8673

8774
@Test
88-
void testGetAllAccounts() throws Exception {
75+
@Order(4)
76+
@DisplayName("Getting all accounts is implemented")
77+
void getAllAccounts() throws Exception {
8978
Account account1 = create("Johnny", "Boy", "jboy@gmail.com");
9079
Account account2 = create("Okko", "Bay", "obay@gmail.com");
9180
accountDao.save(account1);
@@ -96,16 +85,18 @@ void testGetAllAccounts() throws Exception {
9685
.andExpect(jsonPath("$.[*].email").value(hasItems("jboy@gmail.com", "obay@gmail.com")));
9786
}
9887

99-
private Account create(String firstName, String lastName, String email) {
100-
Account account = new Account();
101-
account.setFirstName(firstName);
102-
account.setLastName(lastName);
103-
account.setEmail(email);
104-
return account;
88+
@Test
89+
@Order(5)
90+
@DisplayName("Getting all accounts response status is OK")
91+
void getAccountsResponseStatusCode() throws Exception {
92+
mockMvc.perform(get("/accounts").accept(MediaType.APPLICATION_JSON))
93+
.andExpect(status().isOk());
10594
}
10695

10796
@Test
108-
void testGetById() throws Exception {
97+
@Order(6)
98+
@DisplayName("Getting account by Id with path variable is implemented")
99+
void getById() throws Exception {
109100
Account account = create("Johnny", "Boy", "jboy@gmail.com");
110101
accountDao.save(account);
111102

@@ -118,7 +109,39 @@ void testGetById() throws Exception {
118109
}
119110

120111
@Test
121-
void testRemoveAccount() throws Exception {
112+
@Order(7)
113+
@DisplayName("Creating account returns corresponding HTTP status")
114+
void httpStatusCodeOnCreate() throws Exception {
115+
mockMvc.perform(
116+
post("/accounts")
117+
.contentType(MediaType.APPLICATION_JSON)
118+
.content("{\"firstName\":\"Johnny\", \"lastName\":\"Boy\", \"email\":\"jboy@gmail.com\"}"))
119+
.andExpect(status().isCreated());
120+
}
121+
122+
@Test
123+
@Order(8)
124+
@DisplayName("Creating account returns assigned Id")
125+
void createAccountReturnsAssignedId() throws Exception {
126+
mockMvc.perform(
127+
post("/accounts")
128+
.contentType(MediaType.APPLICATION_JSON)
129+
.content("{\"firstName\":\"Johnny\", \"lastName\":\"Boy\", \"email\":\"jboy@gmail.com\"}"))
130+
.andExpect(jsonPath("$.id").value(1L));
131+
}
132+
133+
private Account create(String firstName, String lastName, String email) {
134+
Account account = new Account();
135+
account.setFirstName(firstName);
136+
account.setLastName(lastName);
137+
account.setEmail(email);
138+
return account;
139+
}
140+
141+
@Test
142+
@Order(9)
143+
@DisplayName("Removing account is implemented")
144+
void removeAccount() throws Exception {
122145
Account account = create("Johnny", "Boy", "jboy@gmail.com");
123146
accountDao.save(account);
124147

@@ -127,14 +150,14 @@ void testRemoveAccount() throws Exception {
127150
}
128151

129152
@Test
130-
void testUpdateAccount() throws Exception {
153+
@Order(10)
154+
@DisplayName("Updating account is implemented")
155+
void updateAccount() throws Exception {
131156
Account account = create("Johnny", "Boy", "jboy@gmail.com");
132157
accountDao.save(account);
133158

134159
mockMvc.perform(put(String.format("/accounts/%d", account.getId())).contentType(MediaType.APPLICATION_JSON)
135160
.content(String.format("{\"id\":\"%d\", \"firstName\":\"Johnny\", \"lastName\":\"Boy\", \"email\":\"johnny.boy@gmail.com\"}", account.getId())))
136161
.andExpect(status().isNoContent());
137162
}
138-
139-
140163
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.bobocode;
2+
3+
import com.bobocode.config.RootConfig;
4+
import com.bobocode.config.WebConfig;
5+
import com.bobocode.dao.impl.InMemoryAccountDao;
6+
import org.junit.jupiter.api.*;
7+
import org.springframework.context.annotation.ComponentScan;
8+
import org.springframework.context.annotation.ComponentScan.Filter;
9+
import org.springframework.context.annotation.Configuration;
10+
import org.springframework.context.annotation.FilterType;
11+
import org.springframework.stereotype.Component;
12+
import org.springframework.stereotype.Controller;
13+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
14+
15+
import java.util.List;
16+
import java.util.stream.Collectors;
17+
import java.util.stream.Stream;
18+
19+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
20+
import static org.junit.jupiter.api.Assertions.assertNotNull;
21+
22+
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
23+
class WebAppConfigurationTest {
24+
25+
@Test
26+
@Order(1)
27+
@DisplayName("RootConfig class is configured properly")
28+
void rootConfigClassIsConfigured() {
29+
Configuration configuration = RootConfig.class.getAnnotation(Configuration.class);
30+
ComponentScan componentScan = RootConfig.class.getAnnotation(ComponentScan.class);
31+
String[] packages = componentScan.basePackages();
32+
if (packages.length == 0) {
33+
packages = componentScan.value();
34+
}
35+
36+
assertNotNull(configuration);
37+
assertNotNull(componentScan);
38+
assertThat(packages).contains("com.bobocode");
39+
40+
Filter[] filters = componentScan.excludeFilters();
41+
List<Class> filteredClasses = getFilteredClasses(filters);
42+
43+
assertThat(filters.length).isEqualTo(2);
44+
assertThat(filters[0].type()).isEqualTo(FilterType.ANNOTATION);
45+
assertThat(filters[1].type()).isEqualTo(FilterType.ANNOTATION);
46+
assertThat(filteredClasses.toArray()).containsExactlyInAnyOrder(EnableWebMvc.class, Controller.class);
47+
}
48+
49+
@Test
50+
@Order(2)
51+
@DisplayName("WebConfig class is configured properly")
52+
void webConfigClassIsConfiguredProperly() {
53+
Configuration configuration = WebConfig.class.getAnnotation(Configuration.class);
54+
ComponentScan componentScan = WebConfig.class.getAnnotation(ComponentScan.class);
55+
EnableWebMvc enableWebMvc = WebConfig.class.getAnnotation(EnableWebMvc.class);
56+
57+
assertNotNull(configuration);
58+
assertNotNull(componentScan);
59+
assertThat(componentScan.basePackages()).contains("com.bobocode.web");
60+
assertNotNull(enableWebMvc);
61+
}
62+
63+
private List<Class> getFilteredClasses(Filter[] filters) {
64+
return Stream.of(filters).flatMap(filter -> Stream.of(filter.value())).collect(Collectors.toList());
65+
}
66+
67+
@Test
68+
@Order(3)
69+
@DisplayName("InMemoryAccountDao class is configured properly")
70+
void inMemoryAccountDaoClassIsConfiguredProperly() {
71+
Component component = InMemoryAccountDao.class.getAnnotation(Component.class);
72+
73+
assertNotNull(component);
74+
assertThat(component.value()).contains("accountDao");
75+
}
76+
}

0 commit comments

Comments
 (0)