Skip to content

Commit 3224da2

Browse files
committed
[Test]: 게시글 조회 테스트 구현
1 parent 5cffaeb commit 3224da2

File tree

1 file changed

+78
-4
lines changed

1 file changed

+78
-4
lines changed

back/src/test/java/com/back/domain/post/controller/PostControllerTest.java

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.back.domain.post.controller;
22

33
import com.back.domain.post.dto.PostRequest;
4+
import com.back.domain.post.dto.PostResponse;
45
import com.back.domain.post.entity.Post;
56
import com.back.domain.post.enums.PostCategory;
67
import com.back.domain.post.repository.PostRepository;
@@ -18,14 +19,18 @@
1819
import org.springframework.beans.factory.annotation.Autowired;
1920
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
2021
import org.springframework.boot.test.context.SpringBootTest;
22+
import org.springframework.http.HttpStatus;
2123
import org.springframework.http.MediaType;
2224
import org.springframework.test.context.ActiveProfiles;
2325
import org.springframework.test.web.servlet.MockMvc;
26+
import org.springframework.test.web.servlet.MvcResult;
2427
import org.springframework.transaction.annotation.Transactional;
2528

2629
import java.time.LocalDateTime;
30+
import java.util.stream.IntStream;
2731

2832
import static org.assertj.core.api.Assertions.assertThat;
33+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
2934
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
3035
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
3136
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@@ -61,6 +66,17 @@ void setUp() {
6166
.birthdayAt(LocalDateTime.of(2000, 3, 1, 0, 0))
6267
.build();
6368
userRepository.save(testUser);
69+
70+
IntStream.rangeClosed(1, 5).forEach(i -> {
71+
postRepository.save(
72+
Post.builder()
73+
.title("목록 게시글 " + i)
74+
.content("목록 내용 " + i)
75+
.category(PostCategory.CHAT)
76+
.user(testUser)
77+
.build()
78+
);
79+
});
6480
}
6581

6682
@Nested
@@ -73,16 +89,20 @@ void success() throws Exception {
7389
// given
7490
PostRequest request = new PostRequest("테스트 게시글", "테스트 내용입니다.", PostCategory.CHAT);
7591

76-
// when & then
77-
mockMvc.perform(post("/api/v1/posts")
92+
// when
93+
MvcResult result = mockMvc.perform(post("/api/v1/posts")
7894
.contentType(MediaType.APPLICATION_JSON)
7995
.content(objectMapper.writeValueAsString(request)))
8096
.andExpect(status().isCreated())
8197
.andExpect(jsonPath("$.title").value("테스트 게시글"))
8298
.andExpect(jsonPath("$.content").value("테스트 내용입니다."))
83-
.andExpect(jsonPath("$.category").value("CHAT"));
99+
.andExpect(jsonPath("$.category").value("CHAT"))
100+
.andReturn();
84101

85-
Post savedPost = postRepository.findAll().get(0);
102+
String responseBody = result.getResponse().getContentAsString();
103+
PostResponse response = objectMapper.readValue(responseBody, PostResponse.class);
104+
105+
Post savedPost = postRepository.findById(response.id()).orElseThrow();
86106
assertThat(savedPost.getTitle()).isEqualTo("테스트 게시글");
87107
assertThat(savedPost.getContent()).isEqualTo("테스트 내용입니다.");
88108
assertThat(savedPost.getCategory()).isEqualTo(PostCategory.CHAT);
@@ -103,4 +123,58 @@ void fail_ValidationError() throws Exception {
103123
.andExpect(jsonPath("$.message").exists());
104124
}
105125
}
126+
127+
@Nested
128+
@DisplayName("게시글 조회")
129+
class GetPost {
130+
131+
@Test
132+
@DisplayName("성공 - 존재하는 게시글 조회")
133+
void success() throws Exception {
134+
// given
135+
Post savedPost = postRepository.save(
136+
Post.builder()
137+
.title("조회 테스트 게시글")
138+
.content("조회 테스트 내용입니다.")
139+
.category(PostCategory.CHAT)
140+
.user(userRepository.findAll().get(0))
141+
.build()
142+
);
143+
144+
// when & then
145+
mockMvc.perform(get("/api/v1/posts/{postId}", savedPost.getId()))
146+
.andExpect(status().isOk())
147+
.andExpect(jsonPath("$.title").value("조회 테스트 게시글"))
148+
.andExpect(jsonPath("$.content").value("조회 테스트 내용입니다."))
149+
.andExpect(jsonPath("$.category").value("CHAT"));
150+
}
151+
152+
@Test
153+
@DisplayName("실패 - 존재하지 않는 게시글 ID")
154+
void fail_NotFound() throws Exception {
155+
// when & then
156+
mockMvc.perform(get("/api/v1/posts/{postId}", 9999L))
157+
.andExpect(status().isNotFound())
158+
.andExpect(jsonPath("$.status").value(HttpStatus.NOT_FOUND.value()))
159+
.andExpect(jsonPath("$.code").value(ErrorCode.POST_NOT_FOUND.getCode()))
160+
.andExpect(jsonPath("$.message").value(ErrorCode.POST_NOT_FOUND.getMessage()))
161+
.andExpect(jsonPath("$.path").value("/api/v1/posts/9999"));
162+
}
163+
}
164+
165+
@Nested
166+
@DisplayName("게시글 목록 조회")
167+
class GetPosts {
168+
169+
@Test
170+
@DisplayName("성공 - 게시글 목록 조회")
171+
void success() throws Exception {
172+
// when & then
173+
mockMvc.perform(get("/api/v1/posts"))
174+
.andExpect(status().isOk())
175+
.andExpect(jsonPath("$.length()").value(5))
176+
.andExpect(jsonPath("$[0].title").value("목록 게시글 1"))
177+
.andExpect(jsonPath("$[1].title").value("목록 게시글 2"));
178+
}
179+
}
106180
}

0 commit comments

Comments
 (0)