Skip to content

Commit 435ab6f

Browse files
authored
[test] PostControllerTest 구현#165
[test] PostControllerTest 구현#165
2 parents 6b832e8 + 2625599 commit 435ab6f

File tree

1 file changed

+267
-0
lines changed

1 file changed

+267
-0
lines changed
Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
package com.back.domain.post.post.controller;
2+
3+
import static org.hamcrest.Matchers.hasSize;
4+
import static org.mockito.ArgumentMatchers.any;
5+
import static org.mockito.ArgumentMatchers.eq;
6+
import static org.mockito.BDDMockito.given;
7+
import static org.mockito.BDDMockito.willDoNothing;
8+
import static org.mockito.Mockito.verify;
9+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
10+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
11+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
12+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
13+
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
14+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
15+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
16+
17+
import com.back.domain.post.post.dto.request.PostCreateRequestDto;
18+
import com.back.domain.post.post.dto.request.PostUpdateRequestDto;
19+
import com.back.domain.post.post.dto.response.PostResponseDto;
20+
import com.back.domain.post.post.enums.PostStatus;
21+
import com.back.domain.post.post.service.PostService;
22+
import com.back.global.jwt.JwtUtil;
23+
import com.back.global.rq.Rq;
24+
import com.fasterxml.jackson.databind.ObjectMapper;
25+
import java.time.LocalDateTime;
26+
import java.util.ArrayList;
27+
import java.util.List;
28+
import org.junit.jupiter.api.DisplayName;
29+
import org.junit.jupiter.api.Test;
30+
import org.springframework.beans.factory.annotation.Autowired;
31+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
32+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
33+
import org.springframework.http.MediaType;
34+
import org.springframework.test.context.bean.override.mockito.MockitoBean;
35+
import org.springframework.test.web.servlet.MockMvc;
36+
37+
@WebMvcTest(PostController.class)
38+
@AutoConfigureMockMvc(addFilters = false)
39+
class PostControllerTest {
40+
41+
@Autowired
42+
private MockMvc mockMvc;
43+
@Autowired
44+
private ObjectMapper objectMapper;
45+
@MockitoBean
46+
private PostService postService;
47+
@MockitoBean
48+
private JwtUtil jwtUtil;
49+
@MockitoBean
50+
private Rq rq;
51+
52+
private PostResponseDto createSampleResponseDto(Long id) {
53+
return new PostResponseDto(
54+
id,
55+
"자유게시판",
56+
"테스트유저" + id,
57+
LocalDateTime.now(),
58+
LocalDateTime.now(),
59+
PostStatus.PUBLIC,
60+
"테스트 제목" + id,
61+
"테스트 내용" + id,
62+
"http://example.com/image.jpg",
63+
"http://example.com/video.mp4",
64+
List.of("태그1", "태그2"),
65+
0, // likeCount
66+
0, // commentCount
67+
0 // viewCount
68+
);
69+
}
70+
71+
@Test
72+
@DisplayName("게시글 작성 API 테스트")
73+
void createPost() throws Exception {
74+
// given
75+
PostCreateRequestDto requestDto = new PostCreateRequestDto(
76+
1L,
77+
PostStatus.PUBLIC,
78+
"테스트 제목1",
79+
"테스트 내용1",
80+
"http://example.com/image1.jpg",
81+
"http://example.com/video1.mp4",
82+
List.of("태그1", "태그2")
83+
);
84+
PostResponseDto responseDto = createSampleResponseDto(1L);
85+
given(postService.createPost(any(PostCreateRequestDto.class))).willReturn(responseDto);
86+
87+
// when & then
88+
mockMvc.perform(post("/posts")
89+
.contentType(MediaType.APPLICATION_JSON)
90+
.content(objectMapper.writeValueAsString(requestDto)))
91+
.andExpect(status().isOk())
92+
.andExpect(jsonPath("$.data.postId").value(1L))
93+
.andExpect(jsonPath("$.data.categoryName").value("자유게시판"))
94+
.andExpect(jsonPath("$.data.userNickName").value("테스트유저1"))
95+
.andExpect(jsonPath("$.data.title").value("테스트 제목1"))
96+
.andExpect(jsonPath("$.data.content").value("테스트 내용1"))
97+
.andExpect(jsonPath("$.data.imageUrl").value("http://example.com/image.jpg"))
98+
.andExpect(jsonPath("$.data.videoUrl").value("http://example.com/video.mp4"))
99+
.andExpect(jsonPath("$.data.tags[0]").value("태그1"))
100+
.andExpect(jsonPath("$.data.tags[1]").value("태그2"))
101+
.andExpect(jsonPath("$.data.likeCount").value(0))
102+
.andExpect(jsonPath("$.data.commentCount").value(0))
103+
.andExpect(jsonPath("$.data.viewCount").value(0))
104+
.andDo(print());
105+
}
106+
107+
@Test
108+
@DisplayName("게시글 다건 조회 API 테스트")
109+
void getAllPosts() throws Exception {
110+
// given
111+
List<PostResponseDto> firstPage = new ArrayList<>();
112+
for (long i = 30; i >= 21; i--) {
113+
firstPage.add(createSampleResponseDto(i));
114+
}
115+
116+
List<PostResponseDto> secondPage = new ArrayList<>();
117+
for (long i = 20; i >= 11; i--) {
118+
secondPage.add(createSampleResponseDto(i));
119+
}
120+
121+
given(postService.getAllPosts(null)).willReturn(firstPage); // 첫 호출(lastId 없음)
122+
given(postService.getAllPosts(21L)).willReturn(secondPage);
123+
124+
// when & then
125+
mockMvc.perform(get("/posts"))
126+
.andExpect(status().isOk())
127+
.andExpect(jsonPath("$.data", hasSize(10)))
128+
.andExpect(jsonPath("$.data[0].postId").value(30))
129+
.andExpect(jsonPath("$.data[0].categoryName").value("자유게시판"))
130+
.andExpect(jsonPath("$.data[0].userNickName").value("테스트유저30"))
131+
.andExpect(jsonPath("$.data[0].title").value("테스트 제목30"))
132+
.andExpect(jsonPath("$.data[0].content").value("테스트 내용30"))
133+
.andExpect(jsonPath("$.data[0].imageUrl").value("http://example.com/image.jpg"))
134+
.andExpect(jsonPath("$.data[0].videoUrl").value("http://example.com/video.mp4"))
135+
.andExpect(jsonPath("$.data[0].tags[0]").value("태그1"))
136+
.andExpect(jsonPath("$.data[0].tags[1]").value("태그2"))
137+
.andExpect(jsonPath("$.data[0].likeCount").value(0))
138+
.andExpect(jsonPath("$.data[0].commentCount").value(0))
139+
.andExpect(jsonPath("$.data[0].viewCount").value(0))
140+
.andDo(print());
141+
142+
mockMvc.perform(get("/posts?lastId=21"))
143+
.andExpect(status().isOk())
144+
.andExpect(jsonPath("$.data", hasSize(10)))
145+
.andExpect(jsonPath("$.data[0].postId").value(20))
146+
.andExpect(jsonPath("$.data[0].categoryName").value("자유게시판"))
147+
.andExpect(jsonPath("$.data[0].userNickName").value("테스트유저20"))
148+
.andExpect(jsonPath("$.data[0].title").value("테스트 제목20"))
149+
.andExpect(jsonPath("$.data[0].content").value("테스트 내용20"))
150+
.andExpect(jsonPath("$.data[0].imageUrl").value("http://example.com/image.jpg"))
151+
.andExpect(jsonPath("$.data[0].videoUrl").value("http://example.com/video.mp4"))
152+
.andExpect(jsonPath("$.data[0].tags[0]").value("태그1"))
153+
.andExpect(jsonPath("$.data[0].tags[1]").value("태그2"))
154+
.andExpect(jsonPath("$.data[0].likeCount").value(0))
155+
.andExpect(jsonPath("$.data[0].commentCount").value(0))
156+
.andExpect(jsonPath("$.data[0].viewCount").value(0))
157+
.andDo(print());
158+
}
159+
160+
@Test
161+
@DisplayName("게시글 단건 조회 API 테스트")
162+
void getPost() throws Exception {
163+
// given
164+
Long postId = 1L;
165+
PostResponseDto responseDto = createSampleResponseDto(postId);
166+
given(postService.getPost(postId)).willReturn(responseDto);
167+
168+
// when & then
169+
mockMvc.perform(get("/posts/{postId}", postId))
170+
.andExpect(status().isOk())
171+
.andExpect(jsonPath("$.data.postId").value(postId))
172+
.andExpect(jsonPath("$.data.categoryName").value("자유게시판"))
173+
.andExpect(jsonPath("$.data.userNickName").value("테스트유저" + postId))
174+
.andExpect(jsonPath("$.data.title").value("테스트 제목" + postId))
175+
.andExpect(jsonPath("$.data.content").value("테스트 내용" + postId))
176+
.andExpect(jsonPath("$.data.imageUrl").value("http://example.com/image.jpg"))
177+
.andExpect(jsonPath("$.data.videoUrl").value("http://example.com/video.mp4"))
178+
.andExpect(jsonPath("$.data.tags[0]").value("태그1"))
179+
.andExpect(jsonPath("$.data.tags[1]").value("태그2"))
180+
.andExpect(jsonPath("$.data.likeCount").value(0))
181+
.andExpect(jsonPath("$.data.commentCount").value(0))
182+
.andExpect(jsonPath("$.data.viewCount").value(0))
183+
.andDo(print());
184+
}
185+
186+
@Test
187+
@DisplayName("게시글 수정 API 테스트")
188+
void updatePost() throws Exception {
189+
// given
190+
Long postId = 1L;
191+
PostUpdateRequestDto requestDto = new PostUpdateRequestDto(
192+
postId,
193+
PostStatus.PUBLIC,
194+
"수정된 제목" + postId,
195+
"수정된 내용" + postId,
196+
"http://example.com/image.jpg",
197+
"http://example.com/video.mp4",
198+
List.of("태그1", "태그2")
199+
);
200+
PostResponseDto responseDto = new PostResponseDto(
201+
postId,
202+
"자유게시판",
203+
"테스트유저" + postId,
204+
LocalDateTime.now(),
205+
LocalDateTime.now(),
206+
PostStatus.PUBLIC,
207+
requestDto.title(),
208+
requestDto.content(),
209+
"http://example.com/image.jpg",
210+
"http://example.com/video.mp4",
211+
List.of("태그1", "태그2"),
212+
0, // likeCount
213+
0, // commentCount
214+
0 // viewCount
215+
);
216+
given(postService.updatePost(eq(1L), any(PostUpdateRequestDto.class))).willReturn(responseDto);
217+
218+
// when & then
219+
mockMvc.perform(patch("/posts/{postId}", postId)
220+
.contentType(MediaType.APPLICATION_JSON)
221+
.content(objectMapper.writeValueAsString(requestDto)))
222+
.andExpect(status().isOk())
223+
.andExpect(jsonPath("$.data.title").value("수정된 제목" + postId))
224+
.andExpect(jsonPath("$.data.content").value("수정된 내용" + postId))
225+
.andExpect(jsonPath("$.data.imageUrl").value("http://example.com/image.jpg"))
226+
.andExpect(jsonPath("$.data.videoUrl").value("http://example.com/video.mp4"))
227+
.andExpect(jsonPath("$.data.tags[0]").value("태그1"))
228+
.andExpect(jsonPath("$.data.tags[1]").value("태그2"))
229+
.andDo(print());
230+
}
231+
232+
@Test
233+
@DisplayName("게시글 삭제 API 테스트")
234+
void deletePost() throws Exception {
235+
// given
236+
Long postId = 1L;
237+
// postService.deletePost(postId)가 호출될 때, 아무런 동작도 하지 않도록 설정 (void 메소드 Mocking)
238+
willDoNothing().given(postService).deletePost(postId);
239+
240+
// when & then
241+
mockMvc.perform(delete("/posts/{postId}", postId))
242+
.andExpect(status().isOk())
243+
.andExpect(jsonPath("$.message").value("success"))
244+
.andExpect(jsonPath("$.data").isEmpty())
245+
.andDo(print());
246+
247+
// 추가 검증: postService의 deletePost 메소드가 정확히 postId=1L 인자로 1번 호출되었는지 확인
248+
verify(postService).deletePost(postId);
249+
}
250+
251+
@Test
252+
@DisplayName("게시글 추천(좋아요) 토글 API 테스트")
253+
void toggleLike() throws Exception {
254+
// given
255+
Long postId = 1L;
256+
willDoNothing().given(postService).toggleLike(postId);
257+
258+
// when & then
259+
mockMvc.perform(post("/posts/{postId}/like", postId))
260+
.andExpect(status().isOk())
261+
.andExpect(jsonPath("$.message").value("success"))
262+
.andExpect(jsonPath("$.data").isEmpty())
263+
.andDo(print());
264+
265+
verify(postService).toggleLike(postId);
266+
}
267+
}

0 commit comments

Comments
 (0)