Support using alwaysDo to automatically document every MockMvc call
The MVC test framework provides an alwaysDo method which allows the configuration of a result handler to be that will be invoked for every call to perform. Previously, this method could not be used for producing documentation snippets as the snippets would overwrite each other. This commit adds support for writing snippets to a parameterized output directory. Two parameters are supported: method name and step. Method name is the name of the currently executing test method. Step is a count of the number of calls to MockMvc.perform that have been made in that method. Closes gh-14
This commit is contained in:
@@ -42,12 +42,12 @@ $ java -jar build/libs/*.jar
|
||||
You can check that the service is up and running by executing a simple request using
|
||||
cURL:
|
||||
|
||||
include::{generated}/index/request.asciidoc[]
|
||||
include::{generated}/index/1/request.asciidoc[]
|
||||
|
||||
This request should yield the following response in the
|
||||
http://stateless.co/hal_specification.html[Hypertext Application Language (HAL)] format:
|
||||
|
||||
include::{generated}/index/response.asciidoc[]
|
||||
include::{generated}/index/1/response.asciidoc[]
|
||||
|
||||
Note the `_links` in the JSON response. They are key to navigating the API.
|
||||
|
||||
@@ -59,26 +59,26 @@ Now that you've started the service and verified that it works, the next step is
|
||||
it to create a new note. As you saw above, the URI for working with notes is included as
|
||||
a link when you perform a `GET` request against the root of the service:
|
||||
|
||||
include::{generated}/index/response.asciidoc[]
|
||||
include::{generated}/index/1/response.asciidoc[]
|
||||
|
||||
To create a note, you need to execute a `POST` request to this URI including a JSON
|
||||
payload containing the title and body of the note:
|
||||
|
||||
include::{generated}/create-note/request.asciidoc[]
|
||||
include::{generated}/creating-a-note/1/request.asciidoc[]
|
||||
|
||||
The response from this request should have a status code of `201 Created` and contain a
|
||||
`Location` header whose value is the URI of the newly created note:
|
||||
|
||||
include::{generated}/create-note/response.asciidoc[]
|
||||
include::{generated}/creating-a-note/1/response.asciidoc[]
|
||||
|
||||
To work with the newly created note you use the URI in the `Location` header. For example,
|
||||
you can access the note's details by performing a `GET` request:
|
||||
|
||||
include::{generated}/get-note/request.asciidoc[]
|
||||
include::{generated}/creating-a-note/2/request.asciidoc[]
|
||||
|
||||
This request will produce a response with the note's details in its body:
|
||||
|
||||
include::{generated}/get-note/response.asciidoc[]
|
||||
include::{generated}/creating-a-note/2/response.asciidoc[]
|
||||
|
||||
Note the `tags` link which we'll make use of later.
|
||||
|
||||
@@ -92,26 +92,26 @@ to tag a note, you must first create the tag.
|
||||
Referring back to the response for the service's index, the URI for working with tags is
|
||||
include as a link:
|
||||
|
||||
include::{generated}/index/response.asciidoc[]
|
||||
include::{generated}/index/1/response.asciidoc[]
|
||||
|
||||
To create a tag you need to execute a `POST` request to this URI, including a JSON
|
||||
payload containing the name of the tag:
|
||||
|
||||
include::{generated}/create-tag/request.asciidoc[]
|
||||
include::{generated}/creating-a-note/3/request.asciidoc[]
|
||||
|
||||
The response from this request should have a status code of `201 Created` and contain a
|
||||
`Location` header whose value is the URI of the newly created tag:
|
||||
|
||||
include::{generated}/create-tag/response.asciidoc[]
|
||||
include::{generated}/creating-a-note/3/response.asciidoc[]
|
||||
|
||||
To work with the newly created tag you use the URI in the `Location` header. For example
|
||||
you can access the tag's details by performing a `GET` request:
|
||||
|
||||
include::{generated}/get-tag/request.asciidoc[]
|
||||
include::{generated}/creating-a-note/4/request.asciidoc[]
|
||||
|
||||
This request will produce a response with the tag's details in its body:
|
||||
|
||||
include::{generated}/get-tag/response.asciidoc[]
|
||||
include::{generated}/creating-a-note/4/response.asciidoc[]
|
||||
|
||||
|
||||
|
||||
@@ -132,24 +132,24 @@ with it.
|
||||
Once again we execute a `POST` request. However, this time, in an array named tags, we
|
||||
include the URI of the tag we just created:
|
||||
|
||||
include::{generated}/create-tagged-note/request.asciidoc[]
|
||||
include::{generated}/creating-a-note/5/request.asciidoc[]
|
||||
|
||||
Once again, the response's `Location` header tells us the URI of the newly created note:
|
||||
|
||||
include::{generated}/create-tagged-note/response.asciidoc[]
|
||||
include::{generated}/creating-a-note/5/response.asciidoc[]
|
||||
|
||||
As before, a `GET` request executed against this URI will retrieve the note's details:
|
||||
|
||||
include::{generated}/get-tagged-note/request-response.asciidoc[]
|
||||
include::{generated}/creating-a-note/6/request-response.asciidoc[]
|
||||
|
||||
To verify that the tag has been associated with the note, we can perform a `GET` request
|
||||
against the URI from the `tags` link:
|
||||
|
||||
include::{generated}/get-tags/request.asciidoc[]
|
||||
include::{generated}/creating-a-note/7/request.asciidoc[]
|
||||
|
||||
The response embeds information about the tag that we've just associated with the note:
|
||||
|
||||
include::{generated}/get-tags/response.asciidoc[]
|
||||
include::{generated}/creating-a-note/7/response.asciidoc[]
|
||||
|
||||
|
||||
|
||||
@@ -159,17 +159,17 @@ An existing note can be tagged by executing a `PATCH` request against the note's
|
||||
a body that contains the array of tags to be associated with the note. We'll used the
|
||||
URI of the untagged note that we created earlier:
|
||||
|
||||
include::{generated}/tag-existing-note/request.asciidoc[]
|
||||
include::{generated}/creating-a-note/8/request.asciidoc[]
|
||||
|
||||
This request should produce a `204 No Content` response:
|
||||
|
||||
include::{generated}/tag-existing-note/response.asciidoc[]
|
||||
include::{generated}/creating-a-note/8/response.asciidoc[]
|
||||
|
||||
When we first created this note, we noted the tags link included in its details:
|
||||
|
||||
include::{generated}/get-note/response.asciidoc[]
|
||||
include::{generated}/creating-a-note/2/response.asciidoc[]
|
||||
|
||||
We can use that link now and execute a `GET` request to see that the note now has a
|
||||
single tag:
|
||||
|
||||
include::{generated}/get-tags-for-existing-note/request-response.asciidoc[]
|
||||
include::{generated}/creating-a-note/9/request-response.asciidoc[]
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
spring.jackson.serialization.indent_output: true
|
||||
Reference in New Issue
Block a user