Improve the API for generating additional snippets

Previously, if alwaysDo was used and the user wanted to generate
one or more additional snippets when calling perform a separate call
to `snippets` was made prior to calling `perform`. This had two
problems:

 - it required the result handler to be stateful (see gh-243)
 - it wasn't clear that the additional snippets would be produced when
   a subsequent call to perform was made

This commit introduces a new API that allows the additional snippets
to be specified within the MockMvc call. The old API has been
deprecated and will be removed in 2.0.

Closes gh-249
This commit is contained in:
Andy Wilkinson
2016-05-25 10:46:39 +01:00
parent cb1f98c601
commit 42353a0b5f
9 changed files with 186 additions and 24 deletions

View File

@@ -46,16 +46,16 @@ public class EveryTestPreprocessing {
// tag::setup[]
private MockMvc mockMvc;
private RestDocumentationResultHandler document;
private RestDocumentationResultHandler documentationHandler;
@Before
public void setup() {
this.document = document("{method-name}", // <1>
this.documentationHandler = document("{method-name}", // <1>
preprocessRequest(removeHeaders("Foo")),
preprocessResponse(prettyPrint()));
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(documentationConfiguration(this.restDocumentation))
.alwaysDo(this.document) // <2>
.alwaysDo(this.documentationHandler) // <2>
.build();
}
@@ -63,10 +63,11 @@ public class EveryTestPreprocessing {
public void use() throws Exception {
// tag::use[]
this.document.snippets( // <1>
links(linkWithRel("self").description("Canonical self link")));
this.mockMvc.perform(get("/")) // <2>
.andExpect(status().isOk());
this.mockMvc.perform(get("/")) // <1>
.andExpect(status().isOk())
.andDo(this.documentationHandler.document( // <2>
links(linkWithRel("self").description("Canonical self link"))
));
// end::use[]
}

View File

@@ -44,16 +44,16 @@ public class EveryTestPreprocessing {
// tag::setup[]
private RequestSpecification spec;
private RestDocumentationFilter document;
private RestDocumentationFilter documentationFilter;
@Before
public void setup() {
this.document = document("{method-name}",
this.documentationFilter = document("{method-name}",
preprocessRequest(removeHeaders("Foo")),
preprocessResponse(prettyPrint())); // <1>
this.spec = new RequestSpecBuilder()
.addFilter(documentationConfiguration(this.restDocumentation))
.addFilter(this.document)// <2>
.addFilter(this.documentationFilter)// <2>
.build();
}
@@ -61,9 +61,9 @@ public class EveryTestPreprocessing {
public void use() throws Exception {
// tag::use[]
this.document.snippets( // <1>
links(linkWithRel("self").description("Canonical self link")));
RestAssured.given(this.spec) // <2>
RestAssured.given(this.spec) // <1>
.filter(this.documentationFilter.document( // <2>
links(linkWithRel("self").description("Canonical self link"))))
.when().get("/")
.then().assertThat().statusCode(is(200));
// end::use[]