Skip to content

Commit f1707fb

Browse files
committed
fix: 테스트케이스 수정
1 parent ed8d2d2 commit f1707fb

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

src/test/java/dmu/dasom/api/domain/news/NewsServiceTest.java

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.List;
2020
import java.util.Optional;
21+
import java.util.stream.Collectors;
2122

2223
import static org.assertj.core.api.Assertions.assertThat;
2324
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -39,8 +40,11 @@ class NewsServiceTest {
3940
@DisplayName("뉴스 전체 조회 - 성공")
4041
void getAllNews_success() {
4142
// Given
42-
NewsEntity news1 = new NewsEntity(1L, "뉴스1", "내용1", List.of(new FileEntity(1L, "image1.jpg", "stored_image1.jpg", "/path/image1.jpg", "image/jpeg", 1024)));
43-
NewsEntity news2 = new NewsEntity(2L, "뉴스2", "내용2", List.of(new FileEntity(2L, "image2.jpg", "stored_image2.jpg", "/path/image2.jpg", "image/jpeg", 2048)));
43+
List<String> imageUrls1 = List.of("/path/image1.jpg");
44+
List<String> imageUrls2 = List.of("/path/image2.jpg");
45+
46+
NewsEntity news1 = new NewsEntity(1L, "뉴스1", "내용1", imageUrls1);
47+
NewsEntity news2 = new NewsEntity(2L, "뉴스2", "내용2", imageUrls2);
4448

4549
when(newsRepository.findAll()).thenReturn(List.of(news1, news2));
4650

@@ -60,7 +64,8 @@ void getAllNews_success() {
6064
void getNewsById_success() {
6165
// Given
6266
Long id = 1L;
63-
NewsEntity news = new NewsEntity(id, "뉴스1", "내용1", List.of(new FileEntity(1L, "image1.jpg", "stored_image1.jpg", "/path/image1.jpg", "image/jpeg", 1024)));
67+
List<String> imageUrls = List.of("/path/image1.jpg");
68+
NewsEntity news = new NewsEntity(id, "뉴스1", "내용1", imageUrls);
6469

6570
when(newsRepository.findById(id)).thenReturn(Optional.of(news));
6671

@@ -82,9 +87,7 @@ void getNewsById_fail() {
8287
when(newsRepository.findById(id)).thenReturn(Optional.empty());
8388

8489
// When & Then
85-
CustomException exception = assertThrows(CustomException.class, () -> {
86-
newsService.getNewsById(id);
87-
});
90+
CustomException exception = assertThrows(CustomException.class, () -> newsService.getNewsById(id));
8891

8992
assertThat(exception.getErrorCode()).isEqualTo(ErrorCode.NOT_FOUND);
9093
verify(newsRepository, times(1)).findById(id);
@@ -95,19 +98,22 @@ void getNewsById_fail() {
9598
void createNews_success() {
9699
// Given
97100
List<Long> fileIds = List.of(1L, 2L);
98-
99101
List<FileEntity> fileEntities = List.of(
100-
new FileEntity(1L, "image1.jpg", "stored_image1.jpg", "/path/image1.jpg", "image/jpeg", 1024),
101-
new FileEntity(2L, "image2.jpg", "stored_image2.jpg", "/path/image2.jpg", "image/jpeg", 2048)
102+
new FileEntity(1L, "image1.jpg", "stored_image1.jpg", "/path/image1.jpg", "image/jpeg", 1024L),
103+
new FileEntity(2L, "image2.jpg", "stored_image2.jpg", "/path/image2.jpg", "image/jpeg", 2048L)
102104
);
103105

106+
List<String> imageUrls = fileEntities.stream()
107+
.map(FileEntity::getFilePath)
108+
.collect(Collectors.toList());
109+
104110
NewsRequestDto requestDto = new NewsRequestDto("새 뉴스", "새 내용", fileIds);
105111

106112
when(fileRepository.findAllById(anyIterable())).thenReturn(fileEntities);
107113
when(newsRepository.save(any(NewsEntity.class)))
108114
.thenAnswer(invocation -> {
109115
NewsEntity news = invocation.getArgument(0);
110-
return new NewsEntity(1L, news.getTitle(), news.getContent(), news.getImages());
116+
return new NewsEntity(1L, news.getTitle(), news.getContent(), imageUrls);
111117
});
112118

113119
// When
@@ -127,14 +133,19 @@ void createNews_success() {
127133
void updateNews_success() {
128134
// Given
129135
Long id = 1L;
130-
NewsEntity existingNews = new NewsEntity(id, "기존 뉴스", "기존 내용", List.of());
136+
List<String> oldImageUrls = List.of("/path/old_image.jpg");
137+
NewsEntity existingNews = new NewsEntity(id, "기존 뉴스", "기존 내용", oldImageUrls);
131138

132139
List<Long> updatedFileIds = List.of(3L, 4L);
133140
List<FileEntity> updatedFiles = List.of(
134-
new FileEntity(3L, "updated_image1.jpg", "stored_updated_image1.jpg", "/path/updated_image1.jpg", "image/jpeg", 1024),
135-
new FileEntity(4L, "updated_image2.jpg", "stored_updated_image2.jpg", "/path/updated_image2.jpg", "image/jpeg", 2048)
141+
new FileEntity(3L, "updated_image1.jpg", "stored_updated_image1.jpg", "/path/updated_image1.jpg", "image/jpeg", 1024L),
142+
new FileEntity(4L, "updated_image2.jpg", "stored_updated_image2.jpg", "/path/updated_image2.jpg", "image/jpeg", 2048L)
136143
);
137144

145+
List<String> updatedImageUrls = updatedFiles.stream()
146+
.map(FileEntity::getFilePath)
147+
.collect(Collectors.toList());
148+
138149
NewsRequestDto updateRequest = new NewsRequestDto("수정된 뉴스", "수정된 내용", updatedFileIds);
139150

140151
when(newsRepository.findById(id)).thenReturn(Optional.of(existingNews));
@@ -146,7 +157,7 @@ void updateNews_success() {
146157
// Then
147158
assertThat(updatedNews.getTitle()).isEqualTo("수정된 뉴스");
148159
assertThat(updatedNews.getContent()).isEqualTo("수정된 내용");
149-
assertThat(updatedNews.getImageUrls()).containsExactly("/path/updated_image1.jpg", "/path/updated_image2.jpg");
160+
assertThat(updatedNews.getImageUrls()).containsExactlyElementsOf(updatedImageUrls);
150161

151162
verify(newsRepository, times(1)).findById(id);
152163
}
@@ -177,12 +188,11 @@ void deleteNews_fail() {
177188
when(newsRepository.findById(id)).thenReturn(Optional.empty());
178189

179190
// When & Then
180-
CustomException exception = assertThrows(CustomException.class, () -> {
181-
newsService.deleteNews(id);
182-
});
191+
CustomException exception = assertThrows(CustomException.class, () -> newsService.deleteNews(id));
183192

184193
assertThat(exception.getErrorCode()).isEqualTo(ErrorCode.NOT_FOUND);
185194
verify(newsRepository, times(1)).findById(id);
186195
verify(newsRepository, never()).delete(any());
187196
}
197+
188198
}

0 commit comments

Comments
 (0)