2023.02.04.SAT

ronglong·2023년 2월 4일
0

[ restDocs ]

  • jsonPath는 루트부터 property 경로 지정해줘야하고, fieldWithPath는 루트부터 경로 지정하면 restDocs 문서 만들기 실패한다고 에러남;;
    한참 헤매다가 혹시?해서 바꿨더니 해결됐다.
mockMvc.perform(
                RestDocumentationRequestBuilders.get("/jmt/member")
                .param("page", "1")
                .param("size", "1")
                .param("sort", "memberId,desc")
        ).andExpect(status().isOk())
                .andExpect(jsonPath("$.data[0].memberId").value(1))
                .andExpect(jsonPath("$.data[0].name").value("영롱"))
                .andExpect(jsonPath("$.data[0].email").value("aaa@naver.com"))
                .andExpect(jsonPath("$.data[0].phone").value("010-1111-2222"))
                .andExpect(jsonPath("$.data[0].stampCount").value(6))
                .andExpect(jsonPath("$.data[0].status").value("활동 회원"))
                .andExpect(jsonPath("$.data[0].createdAt").value("2023-04-03T03:03:00"))
                .andExpect(jsonPath("$.data[0].modifiedAt").value("2023-04-03T03:03:00"))
                .andExpect(jsonPath("$.pageInfo.pageNumber").value(pageRequest.getPageNumber()))
                .andExpect(jsonPath("$.pageInfo.size").value(pageRequest.getPageSize()))
                .andExpect(jsonPath("$.pageInfo.totalPages").value(1))
                .andExpect(jsonPath("$.pageInfo.totalElements").value(1))
                .andDo(document("get-members",
                        preprocessRequest(prettyPrint()),
                        preprocessResponse(prettyPrint()),
                        requestParameters(
                                parameterWithName("page").description("조회 페이지 번호"),
                                parameterWithName("size").description("페이지 내 요소 갯수"),
                                parameterWithName("sort").description("정렬 기준 및 방식. ex) memberId,desc")
                        ),
                        responseFields(
                                fieldWithPath("data[0].memberId").description("회원 번호"),
                                fieldWithPath("data[0].name").description("회원 이름"),
                                fieldWithPath("data[0].email").description("이메일"),
                                fieldWithPath("data[0].phone").description("휴대폰 번호"),
                                fieldWithPath("data[0].stampCount").description("스탬프 갯수"),
                                fieldWithPath("data[0].status").description("회원 상태"),
                                fieldWithPath("data[0].createdAt").description("회원 가입 날짜"),
                                fieldWithPath("data[0].modifiedAt").description("회원 정보 업데이트 날짜"),
                                fieldWithPath("pageInfo.pageNumber").description("페이지 번호"),
                                fieldWithPath("pageInfo.size").description("페이지 내 요소 갯수"),
                                fieldWithPath("pageInfo.totalPages").description("전체 페이지 수"),
                                fieldWithPath("pageInfo.totalElements").description("전체 요소 갯수")
                        )
                ));
  • src/docs/asciidocs 경로 안에 index.adoc 파일을 만들어서 거기에서 snippet을 합쳐서 하나의 html의 베이스를 만든다. -> build하면 html 생성됨.
  • snippet 오버라이드를 통해 custom snippet 만들기
    - src/test/resources/org/springframework/restdocs/templates/asciidoctor 경로에(없으면 만들어서) 커스텀을 원하는 스니핏 파일을 작성하여 오버라이드.
  • 여기서 오류가 생겼는데, 원인은 2가지였다. 알려주신 동기님께 무한한 감사를,,
  • ConstraintDescriptions을 테스트하는 메서드마다 구체적으로 분리해서 작성해주어야 함.
    원래는 클래스 안에 하나 만들어서 MemberDto.class의 유효성 검사 조건을 한 번에 설정하려고 했음.
    근데 생각해보면 그렇게 하면 멤버 클래스마다 같은 이름의 필드라도 다른 유효성 검사 내용을 가질 수 있으므로 오류가 필연적이다.
    void patchMember() throws Exception {
         ConstraintDescriptions requestConstraints = new ConstraintDescriptions(MemberDto.patchDto.class);
         List<String> nameAttribute = requestConstraints.descriptionsForProperty("name");
         List<String> phoneAttribute = requestConstraints.descriptionsForProperty("phone");
         ... 이렇게 변경} ```	
  • 커스텀 스니핏 만들 때, 처음에 복붙으로 입력하면 앞에 공백이 생기는데, 그렇게 되면 인식이 안 된다. 공백 없이 줄 맞춰줘야 함. (shift+tab)
  • index.adoc에서 목차 왼쪽 정렬하는 코드.
    다르게 조합하면 목차가 위쪽 정렬된다. 제목 밑에 공백 있으면 목차 생성 안 됨. 이상하다. 문법이 좀 까다로운 듯.
:sectnums:
:toc: left
:toclevels: 4
:toc-title: JMT
:source-highlighter: prettify

<느낀 점>
restDocs로 API 문서 만드는 거 생각보다 까다로웠다. 어제 밤부터 오늘까지 했음★
진짜 온갖 사이트 들어가서 다 찾아본 듯.
늘 그렇듯이 거의 다 온 것 같은데 마지막에 커스텀 스니핏 작성에서 막혔는데, 갓갓갓 동기님께서 도와주심 ㅠㅠ

내일은 다음 단계로 넘어간다!
만들고 있는 시스템을 확장 & 재구성 & 리팩토링 할 예정.
시큐리티도 적용해보려고 한다.

발등에 불,, 불,, 불,,
프로젝트 진짜 코앞인 거 실화냐🥲
화이팅이다.

0개의 댓글