@AutoConfigureRestDocs 어노테이션을 사용하여 Mock MVC, REST Assured 또는 WebTestClient로 테스트 할 때 Spring REST Docs를 사용할 수있습니다. Spring REST Docs에서 JUnit 룰의 필요성을 제거합니다.
@AutoConfigureRestDocs를 사용하여 기본 출력 디렉토리 (Maven을 사용하는 경우 target / generated-snippets 또는 Gradle을 사용하는 경우 build / generated-snippets)를 무시할 수 있습니다. 문서화 된 URI에 나타나는 호스트, 구성표 및 포트를 구성하는 데 사용할 수도 있습니다.
Spring REST Docs : 테스트 코드 기반으로 RESTful 문서 생성을 돕는 도구. 기본적으로 Asciidoctor를 사용하여 HTML을 생성한다.
Rest Docs Vs. Swagger (RESTful 문서에 대한 명세 보다는 Postman Tool 처럼 특정 API를 쉽게 호출해볼 수 있는 것에 초점)
Auto-configured Spring REST Docs Tests with Mock MVC
@AutoConfigureRestDocs는 Spring REST Docs를 사용하기 위해 MockMvc 빈을 커스터마이즈합니다. 다음 예제와 같이 @Autowired를 사용하여 삽입하고 Mock MVC 및 Spring REST Docs를 사용할 때 평소와 같이 테스트에 사용할 수 있습니다.
Copy import org . junit . Test ;
import org . junit . runner . RunWith ;
import org . springframework . beans . factory . annotation . Autowired ;
import org . springframework . boot . test . autoconfigure . web . servlet . WebMvcTest ;
import org . springframework . http . MediaType ;
import org . springframework . test . context . junit4 . SpringRunner ;
import org . springframework . test . web . servlet . MockMvc ;
import static org . springframework . restdocs . mockmvc . MockMvcRestDocumentation . document ;
import static org . springframework . test . web . servlet . request . MockMvcRequestBuilders . get ;
import static org . springframework . test . web . servlet . result . MockMvcResultMatchers . * ;
@ RunWith ( SpringRunner . class )
@ WebMvcTest ( UserController . class )
@ AutoConfigureRestDocs
public class UserDocumentationTests {
@ Autowired
private MockMvc mvc;
@ Test
public void listUsers () throws Exception {
this . mvc . perform ( get( "/users" ) . accept ( MediaType . TEXT_PLAIN ))
. andExpect ( status() . isOk ())
. andDo ( document( "list-users" ) );
}
}
@AutoConfigureRestDocs의 속성보다 Spring REST Docs 구성을 더 많이 제어해야하는 경우 다음 예제와 같이 RestDocsMockMvcConfigurationCustomizer 빈을 사용할 수 있습니다.
Copy @ TestConfiguration
static class CustomizationConfiguration
implements RestDocsMockMvcConfigurationCustomizer {
@ Override
public void customize ( MockMvcRestDocumentationConfigurer configurer) {
configurer . snippets () . withTemplateFormat ( TemplateFormats . markdown ());
}
}
WebTestClient로 자동 구성된 Spring REST Docs 테스트
@AutoConfigureRestDocs는 WebTestClient와 함께 사용할 수도 있습니다. 다음 예제와 같이 @Autowired를 사용하여 주입 할 수 있으며 @WebFluxTest 및 Spring REST Docs를 사용할 때 평상시처럼 테스트에 사용할 수 있습니다.
Copy import org . junit . jupiter . api . Test ;
import org . junit . runner . RunWith ;
import org . springframework . beans . factory . annotation . Autowired ;
import org . springframework . boot . test . autoconfigure . restdocs . AutoConfigureRestDocs ;
import org . springframework . boot . test . autoconfigure . web . reactive . WebFluxTest ;
import org . springframework . test . context . junit4 . SpringRunner ;
import org . springframework . test . web . reactive . server . WebTestClient ;
import static org . springframework . restdocs . webtestclient . WebTestClientRestDocumentation . document ;
@ RunWith ( SpringRunner . class )
@ WebFluxTest
@ AutoConfigureRestDocs
public class UsersDocumentationTests {
@ Autowired
private WebTestClient webTestClient;
@ Test
void listUsers () {
this . webTestClient . get () . uri ( "/" ) . exchange () . expectStatus () . isOk () . expectBody ()
. consumeWith ( document( "list-users" ) );
}
}
REST Assured로 자동 구성된 Spring REST Docs 테스트
@AutoConfigureRestDocs는 테스트에서 사용할 수있는 Spring REST Docs를 사용하도록 미리 구성된 RequestSpecification 빈을 만든다. 다음 예제와 같이 @Autowired를 사용하여 주입하고 REST Assured 및 Spring REST Docs를 사용하는 경우와 같이 테스트에서 사용할 수 있습니다.
Copy import io . restassured . specification . RequestSpecification ;
import org . junit . Test ;
import org . junit . runner . RunWith ;
import org . springframework . beans . factory . annotation . Autowired ;
import org . springframework . boot . test . autoconfigure . restdocs . AutoConfigureRestDocs ;
import org . springframework . boot . test . context . SpringBootTest ;
import org . springframework . boot . test . context . SpringBootTest . WebEnvironment ;
import org . springframework . boot . web . server . LocalServerPort ;
import org . springframework . test . context . junit4 . SpringRunner ;
import static io . restassured . RestAssured . given ;
import static org . hamcrest . CoreMatchers . is ;
import static org . springframework . restdocs . restassured3 . RestAssuredRestDocumentation . document ;
@ RunWith ( SpringRunner . class )
@ SpringBootTest (webEnvironment = WebEnvironment . RANDOM_PORT )
@ AutoConfigureRestDocs
public class UserDocumentationTests {
@ LocalServerPort
private int port;
@ Autowired
private RequestSpecification documentationSpec;
@ Test
public void listUsers () {
given( this . documentationSpec ) . filter ( document( "list-users" ) ) . when () . port ( this . port ) . get ( "/" ) . then () . assertThat ()
. statusCode ( is( 200 ) );
}
}
@AutoConfigureRestDocs의 속성에 의해 제공되는 것보다 Spring REST Docs 구성을 더 많이 제어해야하는 경우 다음 예제와 같이 RestDocsRestAssuredConfigurationCustomizer 빈을 사용할 수 있습니다.
Copy @ TestConfiguration
public static class CustomizationConfiguration implements RestDocsRestAssuredConfigurationCustomizer {
@ Override
public void customize ( RestAssuredRestDocumentationConfigurer configurer) {
configurer . snippets () . withTemplateFormat ( TemplateFormats . markdown ());
}
}