[최종점검] 스터디 과제 제출#2
Conversation
|
|
||
| [[Member-API]] | ||
| == Member API | ||
| include::api/member.adoc[] |
|
|
||
| class MemberControllerTest extends RestDocsSupport { | ||
| private final MemberService memberService = mock(MemberService.class); | ||
| private static final String MEMBER_POST_URL = "/member"; |
There was a problem hiding this comment.
MEMBER_BASE_URL 이라고 네이밍해도 좋을 것 같아요 ~
| assertTrue(answer.isAnswerType()); | ||
| assertEquals(question.getQuestionContent(), answer.getQuestion().getQuestionContent()); | ||
| } |
|
|
||
| /** | ||
| * memberJpaRepository.findByNameOrThrow에서 import jakarta.persistence.EntityNotFoundException;를 던지면, | ||
| * 실제로는 org.springframework.orm.jpa.JpaObjectRetrievalFailureException가 인식되어 테스트가 실패한다... |
There was a problem hiding this comment.
이거 왜 그런거지 ?!
CustomException 만들어서 던지는게 더 좋은 것 같긴해!
There was a problem hiding this comment.
그러니까요 제말이... 대체 왜냐고 ㅜ 궁금티비
| .name("euna") | ||
| .realAge(24) | ||
| .build(); | ||
| BDDMockito.given(memberJpaRepository.findByNameOrThrow(any(String.class))).willReturn(savedMember); |
There was a problem hiding this comment.
Service는 Mocking해서 단위 Test로 작성했네요!
| import static org.junit.jupiter.api.Assertions.*; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
|
|
||
| @SpringBootTest |
There was a problem hiding this comment.
Mocking 할 때는 @SpringBootTest 말고 @ExtendWith(MockitoExtension.class)만 사용해도 좋을 것 같아요!
There was a problem hiding this comment.
저도 @ExtendWith(MockitoExtension.class) 사용하는 거 좋아하는데 확실히 많이 가볍더라구요! 추천드립니다 ㅎㅎ
| requestFields( | ||
| fieldWithPath("nickName").type(JsonFieldType.STRING).description("회원 닉네임"), | ||
| fieldWithPath("age").type(JsonFieldType.NUMBER).description("회원 나이") | ||
| ), | ||
| responseFields( | ||
| fieldWithPath("code").type(JsonFieldType.NUMBER).description("응답 코드"), | ||
| fieldWithPath("message").type(JsonFieldType.STRING).description("응답 메시지"), | ||
| fieldWithPath("data.memberId").type(JsonFieldType.NUMBER).description("회원 식별자"), | ||
| fieldWithPath("data.nickName").type(JsonFieldType.STRING).description("회원 닉네임"), | ||
| fieldWithPath("data.realAge").type(JsonFieldType.NUMBER).description("회원 나이") | ||
| ) |
There was a problem hiding this comment.
오호 이런 메서드들도 있군요.. (저번에 했었나..?)
알아갑니다 ㅎㅎ
| @ExtendWith(RestDocumentationExtension.class) | ||
| public abstract class RestDocsSupport { | ||
|
|
||
| protected MockMvc mockMvc; | ||
| protected ObjectMapper objectMapper = new ObjectMapper(); | ||
| protected static final String json = "application/json"; | ||
|
|
||
| @BeforeEach | ||
| void setUp(RestDocumentationContextProvider provider) { | ||
| this.mockMvc = MockMvcBuilders | ||
| .standaloneSetup(initializeController()) | ||
| .apply(documentationConfiguration(provider)) | ||
| .build(); | ||
| } |
| answerJpaRepository.deleteAllInBatch(); | ||
| questionJpaRepository.deleteAllInBatch(); |
There was a problem hiding this comment.
그냥 deleteAll() 이 아니라 deleteAllInBatch() 쓴 부분 좋네요 ㅎㅎ
| import static org.junit.jupiter.api.Assertions.*; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
|
|
||
| @SpringBootTest |
There was a problem hiding this comment.
저도 @ExtendWith(MockitoExtension.class) 사용하는 거 좋아하는데 확실히 많이 가볍더라구요! 추천드립니다 ㅎㅎ
Test과제 Rule
조건 : Test 최소 20개 이상
기한
목표