• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

두가지 추가적인 코드 공유드립니다.

20.06.16 23:36 작성 조회수 211

9

1. rest docs 문서에 반환될 값들의 타입을 강하게 걸어서 확실한 테스트를 하고 싶으시다면 아래와 같이 type에 JsonFieldType을 추가하여 테스트할 수 있습니다.

2. 그리고 json의 특정 값이 리턴될 수 있고 안될 수 있는 결과라면.. 예를 들어, response에 description 필드가 있을 경우, 없을 경우를 하나의 문서로 만들기 위해서는 optional()을 붙여주면 됩니다. 결과적으로는  relaxed랑 비슷해졌지만.. 개개의 필드를 설정할 수 있다는 점이 강점인것 같네요.

responseFields(
fieldWithPath("id").type(JsonFieldType.NUMBER).description("event id"),
fieldWithPath("name").type(JsonFieldType.STRING).description("Name of new event"),
fieldWithPath("description").type(JsonFieldType.STRING).description("event description"),
fieldWithPath("beginEnrollmentDateTime").type(JsonFieldType.STRING).description("event enroll start date"),
fieldWithPath("closeEnrollmentDateTime").type(JsonFieldType.STRING).description("devnet enroll end date"),
fieldWithPath("beginEventDateTime").type(JsonFieldType.STRING).description("event start date"),
fieldWithPath("endEventDateTime").type(JsonFieldType.STRING).description("event end date"),
fieldWithPath("location").type(JsonFieldType.STRING).description("event location"),
fieldWithPath("basePrice").type(JsonFieldType.NUMBER).description("event basePrice"),
fieldWithPath("maxPrice").type(JsonFieldType.NUMBER).description("event maxPrice"),
fieldWithPath("limitOfEnrollment").type(JsonFieldType.NUMBER).description("limit enrollment"),
fieldWithPath("free").type(JsonFieldType.BOOLEAN).description("the event is free"),
fieldWithPath("offline").type(JsonFieldType.BOOLEAN).description("the event is offline"),
fieldWithPath("eventStatus").type(JsonFieldType.STRING).description("event status"),

//optional fields
fieldWithPath("_links.self.href").type(JsonFieldType.STRING).description("my href").optional(),
fieldWithPath("_links.query-events.href").type(JsonFieldType.STRING).description("my href").optional(),
fieldWithPath("_links.update-event.href").type(JsonFieldType.STRING).description("my href").optional()
)

답변 2

·

답변을 작성해보세요.

2

좋은 내용 공유 감사합니다.

0

전병준님의 프로필

전병준

2023.02.15

공유 감사합니다 ~