Skip to content

Commit

Permalink
test: #20 answer 수정, 삭제 API 컨트롤러 테스트 코드 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
tnals2384 committed Mar 4, 2024
1 parent d9b1bde commit 0d61c48
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}
13 changes: 13 additions & 0 deletions src/test/java/com/server/capple/support/ControllerTestConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand All @@ -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("나는 와플을 좋아하는 사람이 좋아")
Expand Down

0 comments on commit 0d61c48

Please sign in to comment.