Skip to content

Commit f865167

Browse files
authored
debug : 아이템 도메인 통함 테스트시 통과가 안되던 버그 픽스 (#101)
1 parent 4e788b1 commit f865167

File tree

5 files changed

+192
-61
lines changed

5 files changed

+192
-61
lines changed

backend/src/main/java/com/back/domain/item/controller/ApiV1ItemController.java

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,20 @@ public class ApiV1ItemController {
2727
@PostMapping
2828
@Transactional
2929
@Operation(summary = "테스트용 아이템 생성 ")
30-
public ApiResponse<ItemDto> CreateItem(@RequestBody CreateItemDto createItemDto)
31-
{
32-
ItemDto data = itemService.createItem(new CreateItemDto(
33-
createItemDto.name(),
34-
createItemDto.itemType()
35-
)
36-
);
30+
public ApiResponse<ItemDto> CreateItem(@RequestBody CreateItemDto createItemDto) {
31+
32+
ItemDto data = itemService.createItem(new CreateItemDto(createItemDto.name(), createItemDto.itemType()));
3733
return new ApiResponse<>("200", "아이템 생성 성공", data);
3834
}
3935

4036

4137
@GetMapping
4238
@Transactional
4339
@Operation(summary = "아이템 전체 조회")
44-
public ApiResponse<List<ItemDto>> findAllItems()
45-
{
40+
public ApiResponse<List<ItemDto>> findAllItems() {
41+
4642
try {
47-
List<ItemDto> data =itemService.ReadAllItem();
43+
List<ItemDto> data = itemService.ReadAllItem();
4844
return new ApiResponse<>("200", "아이템 전체 조회 성공", data);
4945
} catch (IllegalArgumentException e) {
5046
return new ApiResponse<>("404", "아이템이 존재하지 않습니다.", null);
@@ -54,25 +50,24 @@ public ApiResponse<List<ItemDto>> findAllItems()
5450
@GetMapping("/{id}")
5551
@Transactional
5652
@Operation(summary = "아이템 단건 조회")
57-
public ApiResponse<ItemDto> findItemById(@PathVariable int id)
58-
{
59-
try {
60-
ItemDto data = itemService.ReadItemById(id);
61-
return new ApiResponse<>("200", "아이템 단건 조회 성공", data);
62-
} catch (IllegalArgumentException e) {
63-
return new ApiResponse<>("404", "아이템이 존재하지 않습니다.", null);
64-
}
53+
public ApiResponse<ItemDto> findItemById(@PathVariable int id) {
54+
55+
try {
56+
ItemDto data = itemService.ReadItemById(id);
57+
return new ApiResponse<>("200", "아이템 단건 조회 성공", data);
58+
} catch (IllegalArgumentException e) {
59+
return new ApiResponse<>("404", "아이템이 존재하지 않습니다.", null);
60+
}
6561

6662
}
6763

6864
@GetMapping("/ItemType/{category}")
6965
@Transactional
7066
@Operation(summary = "아이템 종류별 조회")
71-
public ApiResponse<List<ItemDto>> findAllItems(@PathVariable ItemType category)
72-
{
67+
public ApiResponse<List<ItemDto>> findAllItems(@PathVariable ItemType category) {
7368

7469
try {
75-
List<ItemDto> data =itemService.ReadItemByItemType(category);
70+
List<ItemDto> data = itemService.ReadItemByItemType(category);
7671
return new ApiResponse<>("200", "아이템 단건 조회 성공", data);
7772
} catch (IllegalArgumentException e) {
7873
return new ApiResponse<>("404", "아이템이 존재하지 않습니다.", null);
@@ -84,8 +79,8 @@ public ApiResponse<List<ItemDto>> findAllItems(@PathVariable ItemType category)
8479
@PutMapping("/{id}")
8580
@Transactional
8681
@Operation(summary = "아이템 수정")
87-
public ApiResponse<ItemDto> UpdateItem(@PathVariable int id, @RequestBody ItemDto itemDto)
88-
{
82+
public ApiResponse<ItemDto> UpdateItem(@PathVariable int id, @RequestBody ItemDto itemDto) {
83+
8984
try {
9085
ItemDto data = itemService.UpdateItem(id, itemDto);
9186
return new ApiResponse<>("200", "아이템 단건 조회 성공", data);
@@ -97,8 +92,8 @@ public ApiResponse<ItemDto> UpdateItem(@PathVariable int id, @RequestBody ItemDt
9792
@DeleteMapping("/{id}")
9893
@Transactional
9994
@Operation(summary = "아이템 삭제")
100-
public ApiResponse<ItemDto> DeleteItem(@PathVariable int id)
101-
{
95+
public ApiResponse<ItemDto> DeleteItem(@PathVariable int id) {
96+
10297
try {
10398
itemService.DeleteItem(id);
10499
return new ApiResponse<>("200", "아이템 삭제 성공", null);

backend/src/test/java/com/back/BackApplicationTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import org.junit.jupiter.api.Test;
44
import org.springframework.boot.test.context.SpringBootTest;
5+
import org.springframework.test.context.ActiveProfiles;
56

67
@SpringBootTest
8+
@ActiveProfiles("test")
79
class BackApplicationTests {
810

911
@Test

backend/src/test/java/com/back/domain/item/ApiV1ItemControllerTest.java

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class ApiV1ItemControllerTest {
3636

3737
@Autowired
3838
private EntityManager em;
39+
3940
@Autowired
4041
ItemService itemService;
4142

@@ -55,6 +56,7 @@ void setUp() {
5556
@Test
5657
@DisplayName("READ - 전체 아이템 조회 성공")
5758
void findAllItems_Success() {
59+
5860
CreateItemDto createItemDto1 = new CreateItemDto(
5961
"테스트1", ItemType.AVATAR);
6062
CreateItemDto createItemDto2 = new CreateItemDto(
@@ -84,10 +86,10 @@ void findAllItems_Success() {
8486

8587
@Test
8688
@DisplayName("READ - 아이템 단건 조회 성공")
87-
void findItemsByID_Success()
88-
{
89-
List<Item> itemList = itemRepository.findAll();
90-
int num = 0;
89+
void findItemsByID_Success() {
90+
91+
List<Item> itemList = itemRepository.findAll();
92+
int num = 0;
9193
if (itemList.size() != 0) {
9294
num = itemList.getLast().getId();
9395
}
@@ -108,30 +110,29 @@ void findItemsByID_Success()
108110
restTemplate.exchange(baseUrl, HttpMethod.POST, request3, String.class);
109111

110112
num++;
111-
ResponseEntity<String> response1 = restTemplate.getForEntity(baseUrl+"/"+num, String.class);
113+
ResponseEntity<String> response1 = restTemplate.getForEntity(baseUrl + "/" + num, String.class);
112114
assertThat(response1.getStatusCode()).isEqualTo(HttpStatus.OK);
113115
assertThat(response1.getBody()).contains("테스트1");
114116
assertThat(response1.getBody()).contains(ItemType.AVATAR.toString());
115117

116118
num++;
117-
ResponseEntity<String> response2 = restTemplate.getForEntity(baseUrl+"/"+num, String.class);
119+
ResponseEntity<String> response2 = restTemplate.getForEntity(baseUrl + "/" + num, String.class);
118120
assertThat(response2.getStatusCode()).isEqualTo(HttpStatus.OK);
119121
assertThat(response2.getBody()).contains("테스트2");
120122
assertThat(response2.getBody()).contains(ItemType.FURNITURE.toString());
121123

122124
num++;
123-
ResponseEntity<String> response3 = restTemplate.getForEntity(baseUrl+"/"+num, String.class);
125+
ResponseEntity<String> response3 = restTemplate.getForEntity(baseUrl + "/" + num, String.class);
124126
assertThat(response3.getStatusCode()).isEqualTo(HttpStatus.OK);
125127
assertThat(response3.getBody()).contains("테스트3");
126128
assertThat(response3.getBody()).contains(ItemType.FURNITURE.toString());
127129
}
128130

129131

130-
131132
@Test
132133
@DisplayName("READ - 아이템 타입별 조회 성공")
133-
void findItemsByItemType_Success()
134-
{
134+
void findItemsByItemType_Success() {
135+
135136
CreateItemDto createItemDto1 = new CreateItemDto(
136137
"테스트1", ItemType.AVATAR);
137138
CreateItemDto createItemDto2 = new CreateItemDto(
@@ -149,16 +150,15 @@ void findItemsByItemType_Success()
149150
restTemplate.exchange(baseUrl, HttpMethod.POST, request3, String.class);
150151

151152

152-
153-
ResponseEntity<String> response1 = restTemplate.getForEntity(baseUrl+"/ItemType/AVATAR", String.class);
153+
ResponseEntity<String> response1 = restTemplate.getForEntity(baseUrl + "/ItemType/AVATAR", String.class);
154154
assertThat(response1.getStatusCode()).isEqualTo(HttpStatus.OK);
155155
assertThat(response1.getBody()).contains("테스트1");
156156
assertThat(response1.getBody()).contains(ItemType.AVATAR.toString());
157157
assertThat(response1.getBody()).doesNotContain(ItemType.FURNITURE.toString());
158158
assertThat(response1.getBody()).doesNotContain(ItemType.CLOTHE.toString());
159159

160160

161-
ResponseEntity<String> response2 = restTemplate.getForEntity(baseUrl+"/ItemType/FURNITURE", String.class);
161+
ResponseEntity<String> response2 = restTemplate.getForEntity(baseUrl + "/ItemType/FURNITURE", String.class);
162162
assertThat(response2.getStatusCode()).isEqualTo(HttpStatus.OK);
163163
assertThat(response2.getBody()).contains("테스트2");
164164
assertThat(response2.getBody()).contains("테스트3");
@@ -167,17 +167,14 @@ void findItemsByItemType_Success()
167167
assertThat(response2.getBody()).doesNotContain(ItemType.CLOTHE.toString());
168168

169169

170-
171-
172-
173170
}
174171

175172
@Test
176173
@DisplayName("UPDATE - 아이템 수정 성공")
177-
void updateItem_Success()
178-
{
179-
List<Item> itemList = itemRepository.findAll();
180-
int num = 0;
174+
void updateItem_Success() {
175+
176+
List<Item> itemList = itemRepository.findAll();
177+
int num = 0;
181178
if (itemList.size() != 0) {
182179
num = itemList.getLast().getId();
183180
}
@@ -201,7 +198,7 @@ void updateItem_Success()
201198
CreateItemDto updateDto1 = new CreateItemDto(
202199
"업데이트1", ItemType.CLOTHE);
203200
HttpEntity<CreateItemDto> updateRequest1 = new HttpEntity<>(updateDto1, headers);
204-
ResponseEntity<String> response1 = restTemplate.exchange(baseUrl+"/"+num, HttpMethod.PUT, updateRequest1, String.class);
201+
ResponseEntity<String> response1 = restTemplate.exchange(baseUrl + "/" + num, HttpMethod.PUT, updateRequest1, String.class);
205202
assertThat(response1.getStatusCode()).isEqualTo(HttpStatus.OK);
206203
assertThat(response1.getBody()).contains("업데이트1");
207204
assertThat(response1.getBody()).contains(ItemType.CLOTHE.toString());
@@ -212,7 +209,7 @@ void updateItem_Success()
212209
CreateItemDto updateDto2 = new CreateItemDto(
213210
"업데이트2", ItemType.CLOTHE);
214211
HttpEntity<CreateItemDto> updateRequest2 = new HttpEntity<>(updateDto2, headers);
215-
ResponseEntity<String> response2 = restTemplate.exchange(baseUrl+"/"+num, HttpMethod.PUT, updateRequest2, String.class);
212+
ResponseEntity<String> response2 = restTemplate.exchange(baseUrl + "/" + num, HttpMethod.PUT, updateRequest2, String.class);
216213
assertThat(response2.getStatusCode()).isEqualTo(HttpStatus.OK);
217214
assertThat(response2.getBody()).contains("업데이트2");
218215
assertThat(response2.getBody()).contains(ItemType.CLOTHE.toString());
@@ -224,11 +221,7 @@ void updateItem_Success()
224221
@DisplayName("DELETE - 아이템 삭제 성공")
225222
void DeleteItem_Success() {
226223

227-
List<Item> itemList = itemRepository.findAll();
228-
int num = 0;
229-
if (itemList.size() != 0) {
230-
num = itemList.getLast().getId();
231-
}
224+
232225

233226
CreateItemDto createItemDto1 = new CreateItemDto(
234227
"테스트1", ItemType.AVATAR);
@@ -237,26 +230,37 @@ void DeleteItem_Success() {
237230
CreateItemDto createItemDto3 = new CreateItemDto(
238231
"테스트3", ItemType.FURNITURE);
239232

233+
List<Item> itemList1 = itemRepository.findAll();
234+
235+
240236
HttpEntity<CreateItemDto> request1 = new HttpEntity<>(createItemDto1, headers);
241237
restTemplate.exchange(baseUrl, HttpMethod.POST, request1, String.class);
242-
238+
List<Item> itemList = itemRepository.findAll();
239+
int num = 0;
240+
if (itemList.size() != 0) {
241+
num = itemList.getLast().getId();
242+
}
243+
System.out.println("num = "+num);
243244
HttpEntity<CreateItemDto> request2 = new HttpEntity<>(createItemDto2, headers);
244245
restTemplate.exchange(baseUrl, HttpMethod.POST, request2, String.class);
245246

247+
246248
HttpEntity<CreateItemDto> request3 = new HttpEntity<>(createItemDto3, headers);
247249
restTemplate.exchange(baseUrl, HttpMethod.POST, request3, String.class);
248250

249-
num++;
251+
250252
ResponseEntity<String> response1 = restTemplate.getForEntity(baseUrl + "/" + num, String.class);
251253
assertThat(response1.getStatusCode()).isEqualTo(HttpStatus.OK);
252254
assertThat(response1.getBody()).contains("테스트1");
253255
assertThat(response1.getBody()).contains(ItemType.AVATAR.toString());
254256

257+
255258
restTemplate.delete(baseUrl + "/" + num);
256259
ResponseEntity<String> response11 = restTemplate.getForEntity(baseUrl + "/" + num, String.class);
257260
assertThat(response11.getBody()).contains("404");
258261
assertThat(response11.getBody()).contains("아이템이 존재하지 않습니다.");
259262
}
263+
260264
private static final Logger log = LoggerFactory.getLogger(ApiV1ItemControllerTest.class);
261265

262266
}

backend/src/test/java/com/back/domain/notification/ApiV1NotificationControllerTest.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class ApiV1NotificationControllerTest {
3636

3737
@BeforeEach
3838
void setUp() {
39+
3940
baseUrl = "http://localhost:" + port + "/api/v1/notification";
4041
headers = new HttpHeaders();
4142
headers.setContentType(MediaType.APPLICATION_JSON);
@@ -46,7 +47,7 @@ void setUp() {
4647
void createNotification_Success() {
4748
// given
4849
CreateNotificationDto createDto = new CreateNotificationDto(
49-
100, "알림 생성 테스트", NotificationType.MESSAGE
50+
100, "알림 생성 테스트", NotificationType.MESSAGE
5051
);
5152

5253
// when
@@ -57,7 +58,7 @@ void createNotification_Success() {
5758

5859
// then
5960
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
60-
assertThat(response.getBody()).contains("200-1");
61+
assertThat(response.getBody()).contains("200-1");
6162
assertThat(response.getBody()).contains("알림이 생성되었습니다.");
6263
assertThat(response.getBody()).contains("알림 생성 테스트");
6364
assertThat(response.getBody()).contains("MESSAGE");
@@ -88,7 +89,7 @@ void createNotification_InvalidData() {
8889
void findAllNotifications_Success() {
8990
// given - 테스트 데이터 생성
9091
CreateNotificationDto createDto1 = new CreateNotificationDto(
91-
100, "테스트 알림 1", NotificationType.MESSAGE
92+
100, "테스트 알림 1", NotificationType.MESSAGE
9293
);
9394
CreateNotificationDto createDto2 = new CreateNotificationDto(
9495
101, "테스트 알림 2", NotificationType.MESSAGE
@@ -162,7 +163,7 @@ void findNotificationById_NotFound() {
162163
void updateNotification_Success() {
163164
// given - 테스트 알림 생성
164165
CreateNotificationDto createDto = new CreateNotificationDto(
165-
100, "수정 전 알림", NotificationType.MESSAGE
166+
100, "수정 전 알림", NotificationType.MESSAGE
166167
);
167168

168169
HttpEntity<CreateNotificationDto> createRequest = new HttpEntity<>(createDto, headers);
@@ -191,7 +192,7 @@ void updateNotification_Success() {
191192
void markNotificationAsRead_Success() {
192193
// given - 테스트 알림 생성
193194
CreateNotificationDto createDto = new CreateNotificationDto(
194-
100, "읽음 처리 테스트", NotificationType.MESSAGE
195+
100, "읽음 처리 테스트", NotificationType.MESSAGE
195196
);
196197

197198
HttpEntity<CreateNotificationDto> createRequest = new HttpEntity<>(createDto, headers);
@@ -234,7 +235,7 @@ void updateNotification_NotFound() {
234235
void updateNotification_InvalidData() {
235236
// given
236237
CreateNotificationDto createDto = new CreateNotificationDto(
237-
100, "수정 테스트", NotificationType.MESSAGE
238+
100, "수정 테스트", NotificationType.MESSAGE
238239
);
239240

240241
HttpEntity<CreateNotificationDto> createRequest = new HttpEntity<>(createDto, headers);
@@ -261,7 +262,7 @@ void updateNotification_InvalidData() {
261262
void deleteNotification_Success() {
262263
// given - 테스트 알림 생성
263264
CreateNotificationDto createDto = new CreateNotificationDto(
264-
100, "삭제 테스트 알림", NotificationType.MESSAGE
265+
100, "삭제 테스트 알림", NotificationType.MESSAGE
265266
);
266267

267268
HttpEntity<CreateNotificationDto> createRequest = new HttpEntity<>(createDto, headers);

0 commit comments

Comments
 (0)