Doesn't generate stub using WireMockRestDocs and SpringAutoRestDocs

365 views Asked by At

I'm using Spring Auto REST Docs is an extension to Spring REST Docs to generate API documentation and I'm doing the set up the MockMvc as in the documentation.

As well, the same time I want generate the WireMock Stub with "http://cloud.spring.io/spring-cloud-contract/1.0.x/#_generating_stubs_using_restdocs"

I'm following this examples: https://github.com/spring-cloud-samples/spring-cloud-contract-samples

My problem is when I create a set up custom the WireMock Stub aren't created and when I use the default MockMvc configuration works but I need the custom configuration too.

 @Before
 public void setUp() {
        this.mockMvc = MockMvcBuilders
                .webAppContextSetup(context)
                .alwaysDo(prepareJackson(objectMapper))
                .alwaysDo(document("{class-name}/{method-name}",
                                   preprocessRequest(), commonResponsePreprocessor()))
                .apply(documentationConfiguration(restDocumentation)
                               .uris()
                               .and().snippets()
                               .withDefaults(curlRequest(), httpRequest(), httpResponse(),
                                             requestFields(), responseFields(), pathParameters(),
                                             requestParameters(), description(), methodAndPath(),
                                             section()))
                .build();
}


 @Test
  public void getTemplate() throws Exception {
    this.mockMvc.perform(get("/")
                                 .contentType(MediaType.APPLICATION_JSON)
                                 .accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isOk())
            .andExpect(jsonPath("result", is("success")))
            .andExpect(jsonPath("version", is("0.0.1")))
            .andDo(WireMockRestDocs.verify().stub("getFlapTemplate"))
            .andDo(MockMvcRestDocumentation.document("getFlapTemplate", SpringCloudContractRestDocs.dslContract()));
}

Is it possible to generate the WireMock stub with a custom's configuration?

1

There are 1 answers

0
Florian Benz On

I got it working by adding new WireMockSnippet() to the list of snippets:

 public void setUp() {
        this.mockMvc = MockMvcBuilders
                .webAppContextSetup(context)
                .alwaysDo(prepareJackson(objectMapper))
                .alwaysDo(document("{class-name}/{method-name}",
                                   preprocessRequest(), commonResponsePreprocessor()))
                .apply(documentationConfiguration(restDocumentation)
                               .uris()
                               .and().snippets()
                               .withDefaults(curlRequest(), httpRequest(), httpResponse(),
                                             requestFields(), responseFields(), pathParameters(),
                                             requestParameters(), description(), methodAndPath(),
                                             section(), new WireMockSnippet()))
                .build();
}

It has to be added explicitly, because the auto configuration of Spring Cloud Contract Wiremock only works with Spring REST Docs and not with Spring Auto REST Docs. If run with Spring REST Docs, the following line adds the snippet: https://github.com/spring-cloud/spring-cloud-contract/blob/master/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/restdocs/WireMockRestDocsConfiguration.java#L43