[우아한테크코스] 7월 8일 TIL

1 minute read

[Spring] RestDocs with RestAssured

  1. build.gradle 설정
    ``` plugins { id ‘org.springframework.boot’ version ‘2.5.2’ id ‘io.spring.dependency-management’ version ‘1.0.11.RELEASE’ id “org.asciidoctor.convert” version “1.5.10” id ‘java’ }

group = ‘com.woowacourse’ version = ‘0.0.1-SNAPSHOT’ sourceCompatibility = ‘11’

repositories { mavenCentral() }

ext { snippetsDir = file(‘build/generated-snippets’) }

dependencies { //spring implementation ‘org.springframework.boot:spring-boot-starter-data-jpa’ implementation ‘org.springframework.boot:spring-boot-starter-validation’ implementation ‘org.springframework.boot:spring-boot-starter-web’

//database
runtimeOnly 'mysql:mysql-connector-java'
runtimeOnly 'com.h2database:h2'

testImplementation 'io.rest-assured:rest-assured:3.3.0'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
// testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc'

//restdocs
asciidoctor 'org.springframework.restdocs:spring-restdocs-asciidoctor'
testImplementation 'org.springframework.restdocs:spring-restdocs-restassured' }

test { outputs.dir snippetsDir useJUnitPlatform() }

asciidoctor { inputs.dir snippetsDir dependsOn test }

task createDocument(type: Copy) { dependsOn asciidoctor from file(“build/asciidoc/html5/index.html”) into file(“src/main/resources/static/docs”) }

bootJar { dependsOn createDocument from (“${asciidoctor.outputDir}/html5”) { into ‘static/docs’ } }


2. src/docs/asciidoc/index.adoc  

= ZZIMKKONG Application API Document :doctype: book :icons: font :source-highlighter: highlightjs :toc: left :toclevels: 3 :sectlinks:

include::member.adoc[]

예외처리

Response

[source,http,options=”nowrap”] —- HTTP/1.1 400 Bad Request Content-Type: application/json

{ “message”: “예외 메시지” } —-


3. Test 코드 상단에 어노테이션 추가  
```java
@ExtendWith({RestDocumentationExtension.class, SpringExtension.class})
@AutoConfigureRestDocs
  1. Test 코드 setUp에 메서드 추가
    @BeforeEach
     public void setUp(RestDocumentationContextProvider restDocumentation) {
         RequestSpecification spec = new RequestSpecBuilder()
                 .addFilter(documentationConfiguration(restDocumentation))
                 .build();
         DocumentUtils.setRequestSpecification(spec);
     }
    
  2. Test 코드에 메서드 추가
    RestAssured
     .given(DocumentUtils.getRequestSpecification()).log().all()
     .accept("application/json")
     .filter(document("member", DocumentUtils.getRequestPreprocessor(), DocumentUtils.getResponsePreprocessor()))
     ...
    
  3. build.gradle에 asciidoctor 재생
    build/generated-snippets/기능명/메서드명 에 http-request.adoc, http-response.adoc 기타 등등 추가되었는지 확인

  4. src/docs/asciidoc/member.adoc 수정
    == Member(멤버)
    === 멤버 회원가입
    ==== Request
    include::{snippets}/member/http-request.adoc[]
    ==== Response
    include::{snippets}/member/http-response.adoc[]
    
  5. Test 모두 통과하면 ./gradlew build bootJar 적용

  6. src/main/resources/static/docs에 index.html에 자신의 api가 추가되었는지 확인

  7. application 돌리고 http://localhost:8080/docs/index.html에 접속해서 반영되었는지 확인

gradle 버전은 반드시 7이 아니고 6이어야만 한다!!