#24 simple sns: 댓글 기능 테스트 코드
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
package com.example.sns.controller.request;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class PostCommentRequest {
|
||||
|
||||
private String comment;
|
||||
}
|
||||
@@ -154,4 +154,9 @@ public class PostService {
|
||||
// count like
|
||||
return likeEntityRepository.countByPost(postEntity);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void comment(Integer postId, String username) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.example.sns.controller;
|
||||
|
||||
import com.example.sns.controller.request.PostCommentRequest;
|
||||
import com.example.sns.controller.request.PostCreateRequest;
|
||||
import com.example.sns.controller.request.PostModifyRequest;
|
||||
import com.example.sns.exception.ErrorCode;
|
||||
@@ -258,4 +259,39 @@ public class PostControllerTest {
|
||||
).andDo(print())
|
||||
.andExpect(status().isNotFound());
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithMockUser
|
||||
void 댓글기능() throws Exception {
|
||||
|
||||
mockMvc.perform(post("/api/v1/posts/1/comments")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsBytes(new PostCommentRequest("comment")))
|
||||
).andDo(print())
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithAnonymousUser
|
||||
void 댓글작성시_로그인하지_않은경우() throws Exception {
|
||||
|
||||
mockMvc.perform(post("/api/v1/posts/1/comments")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsBytes(new PostCommentRequest("comment")))
|
||||
).andDo(print())
|
||||
.andExpect(status().isUnauthorized());
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithMockUser
|
||||
void 댓글작성시_게시글이_없는경우() throws Exception {
|
||||
|
||||
doThrow(new SnsApplicationException(ErrorCode.POST_NOT_FOUND)).when(postService).comment(any(), any());
|
||||
|
||||
mockMvc.perform(post("/api/v1/posts/1/comments")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsBytes(new PostCommentRequest("comment")))
|
||||
).andDo(print())
|
||||
.andExpect(status().isNotFound());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user