-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
639 additions
and
0 deletions.
There are no files selected for viewing
139 changes: 139 additions & 0 deletions
139
src/test/java/com/potatocake/everymoment/controller/CommentControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package com.potatocake.everymoment.controller; | ||
|
||
import static org.mockito.ArgumentMatchers.argThat; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.BDDMockito.then; | ||
import static org.mockito.BDDMockito.willDoNothing; | ||
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; | ||
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.potatocake.everymoment.dto.request.CommentRequest; | ||
import com.potatocake.everymoment.entity.Member; | ||
import com.potatocake.everymoment.security.MemberDetails; | ||
import com.potatocake.everymoment.service.CommentService; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.ResultActions; | ||
|
||
@WebMvcTest(CommentController.class) | ||
class CommentControllerTest { | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@Autowired | ||
private ObjectMapper objectMapper; | ||
|
||
@MockBean | ||
private CommentService commentService; | ||
|
||
@Test | ||
@DisplayName("댓글이 성공적으로 수정된다.") | ||
void should_UpdateComment_When_ValidInput() throws Exception { | ||
// given | ||
Long memberId = 1L; | ||
Long commentId = 1L; | ||
Member member = Member.builder() | ||
.id(memberId) | ||
.number(1234L) | ||
.nickname("testUser") | ||
.build(); | ||
MemberDetails memberDetails = new MemberDetails(member); | ||
|
||
CommentRequest request = new CommentRequest(); | ||
request.setContent("Updated comment"); | ||
|
||
willDoNothing().given(commentService).updateComment( | ||
eq(memberId), | ||
eq(commentId), | ||
argThat(req -> req.getContent().equals("Updated comment")) | ||
); | ||
|
||
// when | ||
ResultActions result = mockMvc.perform(patch("/api/comments/{commentId}", commentId) | ||
.with(user(memberDetails)) | ||
.with(csrf()) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(request))); | ||
|
||
// then | ||
result.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.code").value(200)) | ||
.andExpect(jsonPath("$.message").value("success")); | ||
|
||
then(commentService).should().updateComment( | ||
eq(memberId), | ||
eq(commentId), | ||
argThat(req -> req.getContent().equals("Updated comment")) | ||
); | ||
} | ||
|
||
@Test | ||
@DisplayName("댓글이 성공적으로 삭제된다.") | ||
void should_DeleteComment_When_ValidId() throws Exception { | ||
// given | ||
Long memberId = 1L; | ||
Long commentId = 1L; | ||
Member member = Member.builder() | ||
.id(memberId) | ||
.number(1234L) | ||
.nickname("testUser") | ||
.build(); | ||
MemberDetails memberDetails = new MemberDetails(member); | ||
|
||
willDoNothing().given(commentService).deleteComment(memberId, commentId); | ||
|
||
// when | ||
ResultActions result = mockMvc.perform(delete("/api/comments/{commentId}", commentId) | ||
.with(user(memberDetails)) | ||
.with(csrf())); | ||
|
||
// then | ||
result.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.code").value(200)) | ||
.andExpect(jsonPath("$.message").value("success")); | ||
|
||
then(commentService).should().deleteComment(memberId, commentId); | ||
} | ||
|
||
@Test | ||
@DisplayName("댓글 내용이 누락되면 수정에 실패한다.") | ||
void should_FailToUpdate_When_ContentMissing() throws Exception { | ||
// given | ||
Long memberId = 1L; | ||
Long commentId = 1L; | ||
Member member = Member.builder() | ||
.id(memberId) | ||
.number(1234L) | ||
.nickname("testUser") | ||
.build(); | ||
MemberDetails memberDetails = new MemberDetails(member); | ||
|
||
CommentRequest request = new CommentRequest(); | ||
// content 누락 | ||
|
||
// when | ||
ResultActions result = mockMvc.perform(patch("/api/comments/{commentId}", commentId) | ||
.with(user(memberDetails)) | ||
.with(csrf()) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(request))); | ||
|
||
// then | ||
result.andExpect(status().isBadRequest()) | ||
.andExpect(jsonPath("$.code").value(400)); | ||
|
||
then(commentService).shouldHaveNoInteractions(); | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
src/test/java/com/potatocake/everymoment/entity/CommentTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.potatocake.everymoment.entity; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class CommentTest { | ||
|
||
@Test | ||
@DisplayName("댓글 내용이 성공적으로 업데이트된다.") | ||
void should_UpdateContent_When_NewContentProvided() { | ||
// given | ||
Comment comment = Comment.builder() | ||
.content("Original content") | ||
.build(); | ||
String newContent = "Updated content"; | ||
|
||
// when | ||
comment.updateContent(newContent); | ||
|
||
// then | ||
assertThat(comment.getContent()).isEqualTo(newContent); | ||
} | ||
|
||
@Test | ||
@DisplayName("null 내용으로 업데이트하면 기존 내용이 유지된다.") | ||
void should_KeepOriginalContent_When_NullContentProvided() { | ||
// given | ||
Comment comment = Comment.builder() | ||
.content("Original content") | ||
.build(); | ||
|
||
// when | ||
comment.updateContent(null); | ||
|
||
// then | ||
assertThat(comment.getContent()).isEqualTo("Original content"); | ||
} | ||
|
||
} |
Oops, something went wrong.