1

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?

4

1 回答 1

3

我通过添加new WireMockSnippet()到片段列表中使其工作:

 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();
}

它必须明确添加,因为 Spring Cloud Contract Wiremock 的自动配置仅适用于 Spring REST Docs 而不适用于 Spring Auto REST Docs。如果使用 Spring REST Docs 运行,以下行将添加代码片段: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

于 2018-01-04T01:03:55.160 回答