Provide examples for JUnit rule configuration with both Maven and Gradle
Closes gh-162
This commit is contained in:
@@ -227,18 +227,34 @@ documentation snippets for request and resulting response.
|
||||
==== Setting up Spring MVC test
|
||||
|
||||
The first step in generating documentation snippets is to declare a `public`
|
||||
`RestDocumentation` field that's annotated as a JUnit `@Rule` and to provide an `@Before`
|
||||
method that creates a `MockMvc` instance:
|
||||
`RestDocumentation` field that's annotated as a JUnit `@Rule`. The `RestDocumentation`
|
||||
rule is configured with the output directory into which generated snippets should be
|
||||
written. This output directory should match the snippets directory that you have
|
||||
configured in your `build.gradle` or `pom.xml` file.
|
||||
|
||||
For Maven (`pom.xml` that will typically be `target/generated-snippets`:
|
||||
|
||||
[source,java,indent=0]
|
||||
----
|
||||
@Rule
|
||||
public RestDocumentation restDocumentation = new RestDocumentation("target/generated-snippets");
|
||||
----
|
||||
|
||||
And for Gradle (`build.gradle`) it will typically be `build/generated-snippets`:
|
||||
|
||||
[source,java,indent=0]
|
||||
----
|
||||
@Rule
|
||||
public RestDocumentation restDocumentation = new RestDocumentation("build/generated-snippets");
|
||||
----
|
||||
|
||||
Next, provide an `@Before` method that creates a `MockMvc` instance:
|
||||
|
||||
[source,java,indent=0]
|
||||
----
|
||||
include::{examples-dir}/com/example/ExampleApplicationTests.java[tags=mock-mvc-setup]
|
||||
----
|
||||
|
||||
The `RestDocumentation` rule is configured with the output directory into which
|
||||
generated snippets should be written. This output directory should match the snippets
|
||||
directory that you have configured in your `build.gradle` or `pom.xml` file.
|
||||
|
||||
The `MockMvc` instance is configured using a `RestDocumentationMockMvcConfigurer`. An
|
||||
instance of this class can be obtained from the static `documentationConfiguration()`
|
||||
method on `org.springframework.restdocs.mockmvc.MockMvcRestDocumentation`.
|
||||
|
||||
@@ -28,10 +28,9 @@ import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.docu
|
||||
|
||||
public class ExampleApplicationTests {
|
||||
|
||||
// tag::mock-mvc-setup[]
|
||||
@Rule
|
||||
public final RestDocumentation restDocumentation = new RestDocumentation("build/generated-snippets");
|
||||
|
||||
public final RestDocumentation restDocumentation = new RestDocumentation("target/generated-snippets");
|
||||
// tag::mock-mvc-setup[]
|
||||
@Autowired
|
||||
private WebApplicationContext context;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user