Spring RestDocs

알파로그·2024년 1월 29일
0

Spring Boot

목록 보기
53/57
post-custom-banner

✏️ 환경설정

📍 build.gradle

plugins
  id 'org.asciidoctor.jvm.convert' version '3.3.2'
}

configurations {
    asciidoctorExt
}

// snippets 파일이 저장될 경로
ext {
  set('snippetsDir', file("build/generated-snippets"))
}

dependencies {
  asciidoctorExt 'org.springframework.restdocs:spring-restdocs-asciidoctor'
  testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc'
}

tasks.named('test') {
  outputs.dir snippetsDir
}

tasks.named('asciidoctor') {
  inputs.dir snippetsDir
  dependsOn test
}

asciidoctor {
    dependsOn test // asciidoctor 수행전 test 를 수행
    configurations 'asciidoctorExt'
    baseDirFollowsSourceFile()
}

// 자동으로 생성된 문서를 이동
tasks.register('copyApiDocument', Copy) {
    dependsOn asciidoctor
		// 작업 수행 전 기존 문서 삭제
    doFirst {
        delete file("src/main/resources/static/docs")
    }

		// 생성된 문서를 아래 경로로 이동
    from asciidoctor.outputDir
    into file("src/main/resources/static/docs")
}

✏️ 사용하기

📍 MockMvc

  • 기본 세팅
    • @AutoConfigureRestDocs 선언
    • document() : 문서 작업 시작
    • preprocessRequest, Response(prettyPrint()) : 요청, 응답 json 을 출력할 때 줄을 맞춰줌
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessResponse;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;

...

@AutoConfigureRestDocs
class test {

...

mockMvc.perform(MockMvcRequestBuilders
				.get("/restdocs"))
		.andExpect(status().is2xxSuccessful())
		// 문서 작업 시작
    .andDo(document("docs_name",
            preprocessRequest(prettyPrint()),
            preprocessResponse(prettyPrint());
  • request body 가 있는 경우
```java
import static org.springframework.restdocs.cookies.CookieDocumentation.cookieWithName;
import static org.springframework.restdocs.cookies.CookieDocumentation.requestCookies;

...

.andDo(document("docs_name",
    preprocessRequest(prettyPrint()),
    preprocessResponse(prettyPrint()),
    requestCookies(
        cookieWithName("key").description("about")
    )

- request 에 path variable 이 있는경우

```java
  • request 에 cookie 가 있는경우
import static org.springframework.restdocs.cookies.CookieDocumentation.cookieWithName;
import static org.springframework.restdocs.cookies.CookieDocumentation.requestCookies;

...

.andDo(document("docs_name",
    preprocessRequest(prettyPrint()),
    preprocessResponse(prettyPrint()),
    requestCookies(
        cookieWithName("key").description("about")
    )
  • header 가 있는 경우
import static org.springframework.restdocs.headers.HeaderDocumentation.headerWithName;
import static org.springframework.restdocs.headers.HeaderDocumentation.requestHeaders

...

.andDo(document("docs_name",
    preprocessRequest(prettyPrint()),
    preprocessResponse(prettyPrint()),
    requestHeaders(
        headerWithName("key").description("about")
    )
profile
잘못된 내용 PR 환영
post-custom-banner

0개의 댓글