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:
@@ -58,17 +58,17 @@ Then, in each test, any configuration specific to that test can be performed. Fo
|
||||
----
|
||||
include::{examples-dir}/com/example/mockmvc/EveryTestPreprocessing.java[tags=use]
|
||||
----
|
||||
<1> Document the links specific to the resource that is being tested
|
||||
<2> The request and response will be preprocessed due to the use of `alwaysDo` above.
|
||||
<1> The request and response will be preprocessed due to the use of `alwaysDo` above.
|
||||
<2> Document the links specific to the resource that is being tested
|
||||
|
||||
[source,java,indent=0,role="secondary"]
|
||||
.REST Assured
|
||||
----
|
||||
include::{examples-dir}/com/example/restassured/EveryTestPreprocessing.java[tags=use]
|
||||
----
|
||||
<1> Document the links specific to the resource that is being tested
|
||||
<2> The request and response will be preprocessed due to the configuration of the
|
||||
<1> The request and response will be preprocessed due to the configuration of the
|
||||
`RequestSpecification` in the `setUp` method.
|
||||
<2> Document the links specific to the resource that is being tested
|
||||
|
||||
Various built in preprocessors, including those illustrated above, are available via the
|
||||
static methods on `Preprocessors`. See <<Preprocessors, below>> for further details.
|
||||
|
||||
@@ -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[]
|
||||
}
|
||||
|
||||
|
||||
@@ -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[]
|
||||
|
||||
Reference in New Issue
Block a user