Spring REST Docs

Spring MVC Test๋ฅผ ์ด์šฉํ•ด์„œ REST API ๋ฌธ์„œ์˜ ์กฐ๊ฐ(snippets)์„ ์ƒ์„ฑํ•˜๋Š”๋ฐ ๋„์›€์„ ์ฃผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

๊ธฐ๋ณธ์ ์œผ๋กœ Asciidoctor๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ htmlํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๊ณ , ์›ํ•˜๋Š” ๊ฒฝ์šฐ Markdown์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

API๋ฅผ ์ •์˜ํ•˜๋Š”๋ฐ ์‚ฌ์šฉํ•˜๋Š” ๋‹ค๋ฅธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” Swagger๊ฐ€ ์žˆ๋‹ค.

Swagger์™€์˜ ์ฐจ์ด์ 

Swagger๋Š” API๋ฅผ ํ…Œ์ŠคํŠธ ํ•ด๋ณผ ์ˆ˜ ์žˆ๋Š” ํ™”๋ฉด์„ ์ œ๊ณตํ•˜๊ณ  ์‹ค์ œ ์ฝ”๋“œ์— ์–ด๋…ธํ…Œ์ด์…˜์„ ์ถ”๊ฐ€ํ•ด์•ผํ•˜์ง€๋งŒ Spring Rest Docs๋Š” ํ…Œ์ŠคํŠธ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด ์ƒ์„ฑ๋˜๋ฏ€๋กœ ์‹ค์ œ์ฝ”๋“œ์—๋Š” ์˜ํ–ฅ์ด ์—†๋‹ค.

์‚ฌ์šฉ๋ฒ•

  • MockMvc, WebTestClient๋“ฑ์— ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋ฉฐ test์— ์‚ฌ์šฉํ•˜๋Š” mockMvc๋ฅผ ๋งŒ๋“ค๋•Œ documentationConfiguration์„ ์‚ฌ์šฉํ•˜์—ฌ ์ƒ์„ฑํ•˜๊ณ  MockMvc๋กœ ํ…Œ์ŠคํŠธ ์ค‘ andDo๋ฅผ ์ด์šฉํ•˜์—ฌ ์ƒ์„ฑ

  • Spring Boot์—์„œ๋Š” ๊ฐ„๋‹จํžˆ @AutoConfigureRestDocs๋งŒ ๋ถ™์—ฌ์ฃผ๋ฉด ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

Test๋‚ด์šฉ์ด ๋ฐ”๋€Œ๊ฑฐ๋‚˜ ๋‹ค์‹œ ์‹คํ–‰ํ• ๋•Œ๋งˆ๋‹ค Overrideํ•˜์—ฌ ๋ฌธ์„œ๊ฐ€ ๋‹ค์‹œ ์ž‘์„ฑ๋œ๋‹ค.

๋ฉ”์„œ๋“œ

  • andDo(document(โ€œdoc-nameโ€, snippets))

    • snippets

      • links()

      • requestParameters() + parameterWithName()

      • pathParameters() + parametersWithName()

      • requestParts() + partWithname()

      • requestPartBody()

      • requestPartFields()

      • requestHeaders() + headerWithName()

      • requestFields() + fieldWithPath()

      • responseHeaders() + headerWithName()

      • responseFields() + fieldWithPath()

      • ...

        this.mockMvc.perform(post("/api/events")
                    .contentType(MediaType.APPLICATION_JSON)
                    .accept(MediaTypes.HAL_JSON)
                    .content(this.objectMapper.writeValueAsString(event)))
                .andDo(print())
                .andExpect(status().isCreated())
                .andExpect(jsonPath("id").exists())
                .andExpect(header().exists(HttpHeaders.LOCATION))
                .andExpect(header().string(HttpHeaders.CONTENT_TYPE,"application/hal+json;charset=UTF-8"))
                .andExpect(jsonPath("free").value(false))
                .andExpect(jsonPath("offline").value(true))
                .andExpect(jsonPath("eventStatus").value(EventStatus.DRAFT.name()))
                .andExpect(jsonPath("_links.self").exists())
                .andExpect(jsonPath("_links.query-events").exists())
                .andExpect(jsonPath("_links.update-event").exists())
                .andDo(document("create-event",
                        links(
                             linkWithRel("self").description("link to self"),
                             linkWithRel("query-events").description("link to query-events"),
                             linkWithRel("update-event").description("link to update-event")
                        ),
                        requestHeaders(
                                headerWithName(HttpHeaders.ACCEPT).description("accept header"),
                                headerWithName(HttpHeaders.CONTENT_TYPE).description("content type header")
                        ),
                        requestFields(
                            fieldWithPath("name").description("Name of Event"),
                            fieldWithPath("description").description("Description of Event"),
                            fieldWithPath("beginEnrollmentDateTime").description("๋“ฑ๋ก์‹œ์ž‘๊ธฐ๊ฐ„ of Event"),
                            fieldWithPath("closeEnrollmentDateTime").description("๋“ฑ๋ก๋งˆ๊ฐ๊ธฐ๊ฐ„ of Event"),
                            fieldWithPath("beginEventDateTime").description("์‹œ์ž‘๊ธฐ๊ฐ„ of Event"),
                            fieldWithPath("endEventDateTime").description("์ข…๋ฃŒ๊ธฐ๊ฐ„ of Event"),
                            fieldWithPath("location").description("location of Event"),
                            fieldWithPath("basePrice").description("BasePrice of Event"),
                            fieldWithPath("maxPrice").description("MaxPrice of Event"),
                            fieldWithPath("limitOfEnrollment").description("๋“ฑ๋ก ์ œํ•œ of Event")
                        )
                        ,responseHeaders(
                                headerWithName(HttpHeaders.LOCATION).description("location header"),
                                headerWithName(HttpHeaders.CONTENT_TYPE).description("content type header")
                        ),
                        responseFields(
                                fieldWithPath("id").description("identifier of Event"),
                                fieldWithPath("name").description("Name of Event"),
                                fieldWithPath("description").description("Description of Event"),
                                fieldWithPath("beginEnrollmentDateTime").description("๋“ฑ๋ก์‹œ์ž‘๊ธฐ๊ฐ„ of Event"),
                                fieldWithPath("closeEnrollmentDateTime").description("๋“ฑ๋ก๋งˆ๊ฐ๊ธฐ๊ฐ„ of Event"),
                                fieldWithPath("beginEventDateTime").description("์‹œ์ž‘๊ธฐ๊ฐ„ of Event"),
                                fieldWithPath("endEventDateTime").description("์ข…๋ฃŒ๊ธฐ๊ฐ„ of Event"),
                                fieldWithPath("location").description("location of Event"),
                                fieldWithPath("basePrice").description("BasePrice of Event"),
                                fieldWithPath("maxPrice").description("MaxPrice of Event"),
                                fieldWithPath("limitOfEnrollment").description("๋“ฑ๋ก ์ œํ•œ of Event"),
                                fieldWithPath("offline").description("์˜คํ”„๋ผ์ธ์ธ์ง€ of Event"),
                                fieldWithPath("free").description("๋ฌด๋ฃŒ์ธ์ง€ of Event"),
                                fieldWithPath("eventStatus").description("eventStatus of Event"),
                                fieldWithPath("_links.self.href").description("link to self"),
                                fieldWithPath("_links.query-events.href").description("link to query-events"),
                                fieldWithPath("_links.update-event.href").description("link to update-event")
                        )
                        ));
  • Relaxed* : ๋ชจ๋“  ํ•„๋“œ์— ๋Œ€ํ•ด ๊ธฐ์ˆ  ํ•˜์ง€ ์•Š์•„๋„ ๋ฌธ์„œํ™”๋ฅผ ์‹œ์ผœ์ฃผ๊ธฐ ์œ„ํ•œ prefix

    ๋ฌธ์„œ ์ผ๋ถ€๋ถ„๋งŒ ํ…Œ์ŠคํŠธ ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์ง€๋งŒ, ์ •ํ™•ํ•œ ๋ฌธ์„œํ™”๊ฐ€ ๋ถˆ๊ฐ€๋Šฅ ํ•˜๋‹ค.

  • Processor

    • preprocessRequest(prettyPrint())

    • preprocessResponse(prettyPrint())

      configurer.operationPreprocessors().withRequestDefaults(prettyPrint()).withResponseDefaults(prettyPrint());

Customize

jsonํ˜•ํƒœ๋ฅผ ํ•œ์ค„๋กœ ํฌ๋งทํŒ…ํ•˜์ง€ ์•Š์€ ์ฑ„๋กœ responseํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ณด๊ธฐ๊ฐ€ ๋งค์šฐ ๋ถˆํŽธํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ณด๊ธฐ ํŽธํ•˜๊ฒŒ foramattingํ•˜์—ฌ ์ถœ๋ ฅํ•˜๊ฒŒ RestDocksMockMvcConfigurationCustomizer ๊ตฌํ˜„ํ•œ ๋นˆ์„ ๋“ฑ๋ก(๋‚ด๋ถ€์— prettyPrint ์ด์šฉ)ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๊ณ  Testํด๋ž˜์Šค์— @Import(RestDocsConfiguration.class)์„ ํ†ตํ•ด ์ƒ์„ฑํ•œ ์ปค์Šคํ…€ ์„ค์ •์„ ์ ์šฉํ•ด์•ผ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

@TestConfiguration //Test์—๋งŒ ์‚ฌ์šฉํ•˜๋Š” configuration์ด๋‹ค.
public class RestDocsConfiguration {

    @Bean
    public RestDocsMockMvcConfigurationCustomizer restDocsMockMvcConfigurationCustomizer(){
        return new RestDocsMockMvcConfigurationCustomizer() {
            @Override
            public void customize(MockMvcRestDocumentationConfigurer configurer) {
                configurer.operationPreprocessors()
                        .withRequestDefaults(prettyPrint())
                        .withResponseDefaults(prettyPrint());
            }
        }
    }
}
//replace๊ธฐ๋Šฅ ์ด์šฉํ•˜์—ฌ ๋žŒ๋‹ค๋กœ ๋ฐ”๊พธ๋ฉด
@TestConfiguration
public class RestDocsConfiguration {

    @Bean
    public RestDocsMockMvcConfigurationCustomizer restDocsMockMvcConfigurationCustomizer(){
        return configurer -> configurer.operationPreprocessors().withRequestDefaults(prettyPrint())
                .withResponseDefaults(prettyPrint());
    }
}

build

  • buildํ•˜๊ธฐ ์œ„ํ•ด dependecny ์ถ”๊ฐ€

<plugin>
                <groupId>org.asciidoctor</groupId>
                <artifactId>asciidoctor-maven-plugin</artifactId>
                <version>1.5.8</version>
                <executions>
                    <execution>
                        <id>generate-docs</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>process-asciidoc</goal>
                        </goals>
                        <configuration>
                            <backend>html</backend>
                            <doctype>book</doctype>
                        </configuration>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.springframework.restdocs</groupId>
                        <artifactId>spring-restdocs-asciidoctor</artifactId>
                        <version>2.0.5.RELEASE</version>
                    </dependency>
                </dependencies>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.7</version>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>
                                ${project.build.outputDirectory}/static/docs
                            </outputDirectory>
                            <resources>
                                <resource>
                                    <directory>
                                        ${project.build.directory}/generated-docs
                                    </directory>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
  • mainํŒจํ‚ค์ง€ ์•„๋ž˜ asciidoc ํด๋”๋ฅผ ๋งŒ๋“ค์–ด index.adocํŒŒ์ผ ์ƒ์„ฑ

  • maven์˜ package๋กœ build

    ๋นŒ๋“œํ•˜๋ฉด asciidoctor-maven-plugin์ด asciidocํŒŒ์ผ์„ html๋กœ ๋งŒ๋“ค์–ด genreated-docs ์•„๋ž˜ index.html์ด ์ƒ์„ฑ๋œ๋‹ค.

    maven-resources-plugin์— ๋”ฐ๋ผ build๋œ generated-docs์— ์ƒ์„ฑํ•œ ํŒŒ์ผ์„ build์˜ staticํŒŒ์ผ ์•„๋ž˜ ์ƒ์„ฑํ•ด์คŒ์œผ๋กœ์จ spring bootํŠน์„ฑ์„ ์ด์šฉํ•ด ์„œ๋ฒ„์‹คํ–‰์‹œ url๋กœ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.


Reference

https://docs.spring.io/spring-restdocs/docs/2.0.5.RELEASE/reference/html5/

https://narusas.github.io/2018/03/21/Asciidoc-basic.html

Last updated