diff --git a/src/test/java/com/server/capple/domain/answer/controller/AnswerControllerTest.java b/src/test/java/com/server/capple/domain/answer/controller/AnswerControllerTest.java index 8fd68981..a9fcee39 100644 --- a/src/test/java/com/server/capple/domain/answer/controller/AnswerControllerTest.java +++ b/src/test/java/com/server/capple/domain/answer/controller/AnswerControllerTest.java @@ -15,7 +15,7 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doReturn; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -52,4 +52,50 @@ public void createAnswerTest() throws Exception { .andExpect(jsonPath("$.message").value("요청에 성공하였습니다.")) .andExpect(jsonPath("$.result.answerId").value(1L)); } + + @Test + @DisplayName("답변 수정 API 테스트") + public void updateAnswerTest() throws Exception { + //given + final String url = "/answers/{answerId}"; + + AnswerRequest request = getAnswerRequest(); + AnswerResponse.AnswerId response = new AnswerResponse.AnswerId(1L); + + doReturn(response).when(answerService).updateAnswer(any(Member.class), any(Long.class), any(AnswerRequest.class)); + + //when + ResultActions resultActions = this.mockMvc.perform(patch(url, answer.getId()) + .contentType(MediaType.APPLICATION_JSON_VALUE) + .content(objectMapper.writeValueAsString(request))); + + //then + resultActions.andExpect(status().isOk()) + .andDo(print()) + .andExpect(jsonPath("$.code").value("COMMON200")) + .andExpect(jsonPath("$.message").value("요청에 성공하였습니다.")) + .andExpect(jsonPath("$.result.answerId").value(1L)); + } + + @Test + @DisplayName("답변 삭제 API 테스트") + public void deleteAnswerTest() throws Exception { + //given + final String url = "/answers/{answerId}"; + + AnswerResponse.AnswerId response = new AnswerResponse.AnswerId(1L); + + doReturn(response).when(answerService).deleteAnswer(any(Member.class), any(Long.class)); + + //when + ResultActions resultActions = this.mockMvc.perform(delete(url, answer.getId()) + .contentType(MediaType.APPLICATION_JSON_VALUE)); + + //then + resultActions.andExpect(status().isOk()) + .andDo(print()) + .andExpect(jsonPath("$.code").value("COMMON200")) + .andExpect(jsonPath("$.message").value("요청에 성공하였습니다.")) + .andExpect(jsonPath("$.result.answerId").value(1L)); + } } diff --git a/src/test/java/com/server/capple/support/ControllerTestConfig.java b/src/test/java/com/server/capple/support/ControllerTestConfig.java index 8cf9053b..5f22c01f 100644 --- a/src/test/java/com/server/capple/support/ControllerTestConfig.java +++ b/src/test/java/com/server/capple/support/ControllerTestConfig.java @@ -2,6 +2,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.server.capple.domain.answer.dto.AnswerRequest; +import com.server.capple.domain.answer.entity.Answer; import com.server.capple.domain.member.entity.Member; import com.server.capple.domain.question.entity.Question; import com.server.capple.domain.question.entity.QuestionStatus; @@ -22,11 +23,13 @@ public abstract class ControllerTestConfig { protected ObjectMapper objectMapper; protected Member member; protected Question question; + protected Answer answer; @BeforeEach public void setUp() { member = createMember(); question = createQuestion(); + answer = createAnswer(); } protected Member createMember() { @@ -45,6 +48,16 @@ protected Question createQuestion() { .build(); } + protected Answer createAnswer() { + return Answer.builder() + .id(1L) + .content("나는 무자비한 사람이 좋아") + .tags("#무자비 #와플 ") + .question(question) + .member(member) + .build(); + } + protected AnswerRequest getAnswerRequest() { return AnswerRequest.builder() .answer("나는 와플을 좋아하는 사람이 좋아")