Class SpringCloudContractRestDocs


  • public final class SpringCloudContractRestDocs
    extends Object
    Convenience class for setting up RestDocs to generate a Snippet with Spring Cloud Contract DSL. Example usage:
     @RunWith(SpringRunner.class)
     @SpringBootTest
     @AutoConfigureRestDocs(outputDir = "target/snippets")
     @AutoConfigureMockMvc
     public class ContractRestDocsApplicationTests {
    
            @Autowired
            private MockMvc mockMvc;
    
            @Test
            public void contextLoads() throws Exception {
         this.mockMvc.perform(post("/foo")
              .accept(MediaType.APPLICATION_PDF)
              .accept(MediaType.APPLICATION_JSON)
              .contentType(MediaType.APPLICATION_JSON)
              .content("{\"foo\": 23 }"))
         .andExpect(status().isOk())
         .andExpect(content().string("bar"))
         // first WireMock
         .andDo(WireMockRestDocs.verify()
              .jsonPath("$[?(@.foo >= 20)]")
              .contentType(MediaType.valueOf("application/json")))
         // then Contract DSL documentation
         .andDo(document("index", SpringCloudContractRestDocs.dslContract()));
            }
     
    which creates a file "target/snippets/contracts/index.groovy" and a standard documentation entitled `dsl-contract.adoc` containing that contract.
    Since:
    1.0.4
    Author:
    Marcin Grzejszczak
    • Method Detail

      • dslContract

        public static org.springframework.restdocs.snippet.Snippet dslContract()
        Returns a new Snippet that will document Spring Cloud Contract DSL for the API operation.
        Returns:
        the snippet that will document the Spring Cloud Contract DSL
      • dslContract

        public static org.springframework.restdocs.snippet.Snippet dslContract​(Map<String,​Object> attributes)
        Returns a new Snippet that will document the Spring Cloud Contract DSL for the API operation. The given attributes will be available during snippet generation.
        Parameters:
        attributes - the attributes
        Returns:
        the snippet that will document the Spring Cloud Contract DSL