我正在使用Spring Auto REST Docs是Spring REST Docs的扩展,用于生成API文档,我正在按照文档中的方式设置MockMvc。
同样,我想用“http://cloud.spring.io/spring-cloud-contract/1.0.x/#_generating_stubs_using_restdocs”生成WireMock存根
我正在关注这些示例:https://github.com/spring-cloud-samples/spring-cloud-contract-samples
我的问题是当我创建一个设置自定义时,没有创建WireMock存根,当我使用默认的MockMvc配置时,我也需要自定义配置。
@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()));
}
是否可以使用自定义配置生成WireMock存根?
答案 0 :(得分:1)
我通过将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