fieldWithPath()… 메소드…란?JSON 데이터와 대응되는 요청 데이터와 응답 데이터의 각 프로퍼티에 대한 내용을 문서화해주는 역할을 한다.
ignored() 가 적용된 부분을 봤을 때 좀 의아했다.fieldWithPath() 를 테스트 코드에 추가했다는 건 문서화하기 위해서 아닌가?
굳이 fieldWithPath() 를 사용해서 프로퍼티 설명 열심히 추가한 뒤에
왜 또 ignored() 로 문서화에서 제외하는 건데…?
의문이 들었던 소스코드는 다음과 같았다.
// then
actions.andExpect(status().isOk())
.andExpect(jsonPath("$.data.phone").value(patch.getPhone()))
.andDo(document("patch-member",
getRequestPreProcessor(),
getResponsePreProcessor(),
pathParameters( // 1
parameterWithName("member-id").description("회원 식별자")
),
requestFields(
List.of(
**fieldWithPath("memberId").type(JsonFieldType.NUMBER).description("회원 식별자").ignored(), // 2**
fieldWithPath("name").type(JsonFieldType.STRING).description("이름").optional(), // 3
fieldWithPath("phone").type(JsonFieldType.STRING).description("휴대폰 번호").optional(),
fieldWithPath("memberStatus").type(JsonFieldType.STRING).description("회원 상태: MEMBER_ACTIVE / MEMBER_SLEEP / MEMBER_QUIT").optional()
)
),
responseFields( // 4
List.of(
fieldWithPath("data").type(JsonFieldType.OBJECT).description("결과 데이터"),
fieldWithPath("data.memberId").type(JsonFieldType.NUMBER).description("회원 식별자"), // 5
fieldWithPath("data.email").type(JsonFieldType.STRING).description("이메일"),
fieldWithPath("data.name").type(JsonFieldType.STRING).description("이름"),
fieldWithPath("data.phone").type(JsonFieldType.STRING).description("휴대폰 번호"),
fieldWithPath("data.memberStatus").type(JsonFieldType.STRING).description("회원 상태: 활동중 / 휴면 상태 / 탈퇴 상태"),
fieldWithPath("data.stamp").type(JsonFieldType.NUMBER).description("스탬프 갯수")
)
)
));
memberId필드는 Patch 요청에서Path Variable로 전달되는 데이터이지,Request Body로 전달되는 데이터가 아니다.
바로 위의 pathParameters 부분에 이미 member-id 정보를 추가하여 문서화를 해주었다.
근데 왜 requestFields 에서 굳이 또 문서화 코드를 추가하고 다시 ignored() 를 해주는지에 대한 의문이 들었던 것이다.
일단 이 상태로 테스트를 진행했을 때 잘 통과했고 문서스니펫도 잘 생성이 됐다.