#24 simple sns: 댓글 기능 테스트 코드

This commit is contained in:
haerong22
2022-11-23 00:56:49 +09:00
parent 02fd021a07
commit c4bb71e8e4
3 changed files with 52 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
package com.example.sns.controller.request;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class PostCommentRequest {
private String comment;
}

View File

@@ -154,4 +154,9 @@ public class PostService {
// count like
return likeEntityRepository.countByPost(postEntity);
}
@Transactional
public void comment(Integer postId, String username) {
}
}

View File

@@ -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());
}
}