Skip to content

Instantly share code, notes, and snippets.

@vbsteven
Created May 1, 2019 14:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vbsteven/f0b2e717dd14a9ba46076cdfae037604 to your computer and use it in GitHub Desktop.
Save vbsteven/f0b2e717dd14a9ba46076cdfae037604 to your computer and use it in GitHub Desktop.
Spring Rest Docs documentationConfiguration with JUnit5 and Kotlin
package io.license.core.restdocs
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
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.restdocs.RestDocumentationContextProvider
import org.springframework.restdocs.headers.HeaderDocumentation.headerWithName
import org.springframework.restdocs.headers.HeaderDocumentation.requestHeaders
import org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document
import org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration
import org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessResponse
import org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint
import org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity
import org.springframework.test.context.ActiveProfiles
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
import org.springframework.test.web.servlet.result.MockMvcResultMatchers
import org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder
import org.springframework.test.web.servlet.setup.MockMvcBuilders
import org.springframework.web.context.WebApplicationContext
@SpringBootTest
@AutoConfigureRestDocs
@ActiveProfiles("dev", "test")
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class CustomerRestDocs {
lateinit var mvc: MockMvc
@Autowired
lateinit var restDocumentationContextProvider: RestDocumentationContextProvider
@Autowired
lateinit var webApplicationContext: WebApplicationContext
internal val BEARER_HEADER = "Bearer HUm2W6vzhKda_OpmbsLIdA"
@BeforeAll
fun before() {
this.mvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
.apply<DefaultMockMvcBuilder>(documentationConfiguration(restDocumentationContextProvider).uris()
.withScheme("https")
.withHost("api.license.io")
.withPort(443)
)
.apply<DefaultMockMvcBuilder>(springSecurity())
.build()
}
@Test
fun listCustomers() {
this.mvc.perform(MockMvcRequestBuilders.get("/api/v1/customers")
.header("Authorization", BEARER_HEADER)
.accept("application/hal+json"))
.andExpect(MockMvcResultMatchers.status().isOk)
.andDo(document("list-customers",
preprocessResponse(prettyPrint()),
requestHeaders(
headerWithName("Authorization").description("Bearer token credentials"))
))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment