-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[week4,5] howoldareu 리팩토링 및 테스트 코드 15개 작성 #1
base: main
Are you sure you want to change the base?
Changes from all commits
528cd3c
2794a89
fee9f40
231c1c2
2abc44c
f64a6ee
0ae532a
aff69d0
464b44a
7588d93
444b168
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
package com.sopt.Server.controller.request; | ||
|
||
import lombok.Builder; | ||
|
||
import java.util.List; | ||
|
||
@Builder | ||
public record AnswerListRequestDTO(String name, List<AnswerRequestDTO> results) { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,25 @@ | ||
package com.sopt.Server.domain; | ||
|
||
import jakarta.persistence.*; | ||
import lombok.Getter; | ||
import lombok.*; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@Entity | ||
@Getter | ||
@Table(name = "QUESTIONS") | ||
public class Question { | ||
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long questionId; | ||
private String questionContent; | ||
|
||
@Builder | ||
public Question(String questionContent) { | ||
this.questionContent = questionContent; | ||
} | ||
|
||
@OneToMany(mappedBy = "question") | ||
private List<Answer> answers = new ArrayList<>(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package com.sopt.Server.controller; | ||
|
||
import com.sopt.Server.common.ApiResponse; | ||
import com.sopt.Server.controller.request.MemberPostRequest; | ||
import com.sopt.Server.controller.response.MemberGetResponse; | ||
import com.sopt.Server.domain.Member; | ||
import com.sopt.Server.exception.Success; | ||
import com.sopt.Server.service.MemberService; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.web.servlet.ResultActions; | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; | ||
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.BDDMockito.given; | ||
import static org.mockito.Mockito.when; | ||
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; | ||
|
||
class MemberControllerTest extends MockManager{ | ||
|
||
@InjectMocks | ||
private MemberController memberController; | ||
|
||
@Mock | ||
private MemberService memberService; | ||
|
||
@BeforeEach | ||
public void init() { | ||
mockMvc = MockMvcBuilders.standaloneSetup(memberController).build(); | ||
} | ||
|
||
// ai가 나보다 잘짠다.. | ||
@Test | ||
@DisplayName("멤버를 저장할 수 있다.") | ||
void saveMember_validRequest_returnsSuccess() { | ||
// given | ||
Member member = Member.builder().name("정원").realAge(20).build(); | ||
MemberPostRequest request = new MemberPostRequest("정원", 10); | ||
MemberGetResponse response = MemberGetResponse.of(member); | ||
when(memberService.saveMember(request)).thenReturn(response); | ||
|
||
// when | ||
ApiResponse<MemberGetResponse> result = memberController.saveMember(request); | ||
|
||
// then | ||
assertEquals(Success.CREATE_MEMBER_SUCCESS.getHttpStatus(), result.getCode()); | ||
assertEquals(Success.CREATE_MEMBER_SUCCESS.getMessage(), result.getMessage()); | ||
assertEquals(response, result.getData()); | ||
Comment on lines
+54
to
+56
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JUnit으로 Test Code 쓰셨네요 ... |
||
} | ||
|
||
@Test | ||
@DisplayName("멤버를 저장할 수 있다.") | ||
void saveMemberController() throws Exception { | ||
// given | ||
MemberPostRequest request = new MemberPostRequest("정원", 20); | ||
MemberGetResponse response = new MemberGetResponse(1L, "정원", 20); | ||
|
||
given(memberService.saveMember(any(MemberPostRequest.class))) | ||
.willReturn(response); | ||
|
||
// when | ||
ResultActions resultActions = mockMvc.perform( | ||
MockMvcRequestBuilders.post("/member") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(request)) | ||
); | ||
|
||
// then | ||
resultActions.andExpect(status().isOk()) | ||
.andDo(print()) | ||
.andExpect(jsonPath("$.data.name").value(response.name())) | ||
.andExpect(jsonPath("$.data.realAge").value(response.realAge())); | ||
} | ||
|
||
@Test | ||
@DisplayName("멤버를 조회할 수 있다.") | ||
void getAllResultsController() throws Exception { | ||
// given | ||
|
||
MemberPostRequest request = new MemberPostRequest("정원", 20); | ||
MemberGetResponse response = new MemberGetResponse(1L, "정원", 20); | ||
|
||
given(memberService.getMember(any(MemberPostRequest.class))) | ||
.willReturn(response); | ||
// when | ||
ResultActions resultActions = mockMvc.perform( | ||
MockMvcRequestBuilders.get("/member") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(request)) | ||
); | ||
|
||
// then | ||
resultActions.andExpect(status().isOk()) | ||
.andDo(print()) | ||
.andExpect(jsonPath("$.data.name").value(response.name())) | ||
.andExpect(jsonPath("$.data.realAge").value(response.realAge())); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.sopt.Server.controller; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
//@ExtendWith(SpringExtension.class) // JUnit4 RunWith(SpringRunner.class) 테스트 실행 방법을 확장할 때 사용, ApplicationContext만들고 관리하는 작업을 설정된 class로 사용 | ||
@ExtendWith(MockitoExtension.class) | ||
public abstract class MockManager { | ||
protected MockMvc mockMvc; | ||
protected ObjectMapper objectMapper = new ObjectMapper(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.sopt.Server.controller; | ||
|
||
import com.sopt.Server.controller.response.GetQuestionResponseDTO; | ||
import com.sopt.Server.service.QuestionService; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.springframework.test.web.servlet.ResultActions; | ||
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||
|
||
import java.util.List; | ||
|
||
import static org.mockito.Mockito.doReturn; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
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; | ||
|
||
class QuestionControllerTest extends MockManager { | ||
@InjectMocks | ||
private QuestionController questionController; | ||
|
||
@Mock | ||
QuestionService questionService; | ||
|
||
@BeforeEach | ||
public void init() { | ||
mockMvc = MockMvcBuilders.standaloneSetup(questionController).build(); | ||
} | ||
|
||
@Test | ||
@DisplayName("질문을 조회할 수 있다.") | ||
void getQuestionController() throws Exception { | ||
// given | ||
GetQuestionResponseDTO response1 = GetQuestionResponseDTO.of( | ||
1L, | ||
"Question 1" | ||
); | ||
GetQuestionResponseDTO response2 = GetQuestionResponseDTO.of( | ||
2L, | ||
"Question 2" | ||
); | ||
|
||
// given(questionService.getQuestionResponseDTOList()).willReturn(List.of(response1, response2)); | ||
doReturn(List.of(response1, response2)).when(questionService) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 배워갑니다 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 22 |
||
.getQuestionResponseDTOList(); | ||
|
||
// when | ||
ResultActions resultActions = mockMvc.perform(get("/question")); | ||
|
||
// then | ||
resultActions.andExpect(status().isOk()) | ||
.andDo(print()) | ||
.andExpect(jsonPath("$.data[0].questionId").value(response1.questionId())) | ||
.andExpect(jsonPath("$.data[0].questionContent").value(response1.questionContent())) | ||
.andExpect(jsonPath("$.data[1].questionId").value(response2.questionId())) | ||
.andExpect(jsonPath("$.data[1].questionContent").value(response2.questionContent())); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
??