Provide test auto-configuration for Spring REST Docs

This commit introduces a new annotation, @AutoConfigureRestDocs,
which can be used to enable auto-configuration of Spring REST Docs.
The auto-configuration removes the need to use Spring REST Docs' JUnit
rule and will automatically configure MockMvc. Combined with the new
auto-configuration for MockMvc it allows a test class to be free of
boilerplate configuration:

@RunWith(SpringRunner.class)
@WebMvcTest
@AutoConfigureRestDocs(outputDir = "target/generated-snippets",
        uriScheme = "https", uriHost = "api.example.com",
        uriPort = 443)
public class ExampleDocumentationTests {

    @Autowired
    private MockMvc mvc;

    @Test
    public void documentIndex() {
        // …
    }

}

For more advanced customization a RestDocsMockMvcConfigurationCustomizer
bean can be used.

If a RestDocumentationResultHandler is found in the context, it will
be passed to the ConfigurableMockMvcBuilder's alwaysDo method as part
of its customization.

Closes gh-5563
This commit is contained in:
Andy Wilkinson
2016-04-04 15:41:13 +01:00
parent f99bfccd51
commit eb3180d581
20 changed files with 854 additions and 77 deletions

View File

@@ -4744,6 +4744,84 @@ database you can use the `@AutoConfigureTestDatabase` annotation:
[[boot-features-testing-spring-boot-applications-testing-autoconfigurd-rest-docs]]
==== Auto-configured Spring REST Docs tests
Test `@AutoConfigureRestDocs` annotation can be used if you want to use Spring REST Docs
in your tests. It will automatically configure `MockMvc` to use Spring REST Docs and
removes the need for Spring REST Docs' JUnit rule.
[source,java,indent=0]
----
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@RunWith(SpringRunner.class)
@WebMvcTest(UserController.class)
@AutoConfigureRestDocs("target/generated-snippets")
public class UserDocumentationTests {
@Autowired
private MockMvc mvc;
@Test
public void listUsers() throws Exception {
this.mvc.perform(get("/users").accept(MediaType.TEXT_PLAIN))
.andExpect(status().isOk())
.andDo(document("list-users"));
}
}
----
In addition to configuring the output directory, `@AutoConfigureRestDocs` can also
configure the host, scheme, and port that will appear in any documented URIs. If you
require more control over Spring REST Docs' configuration a
`RestDocsMockMvcConfigurationCustomizer` bean can be used:
[source,java,indent=0]
----
@TestConfiguration
static class CustomizationConfiguration
implements RestDocsMockMvcConfigurationCustomizer {
@Override
public void customize(MockMvcRestDocumentationConfigurer configurer) {
configurer.snippets().withTemplateFormat(TemplateFormats.markdown());
}
}
----
If you want to make use of Spring REST Docs' support for a parameterized output directory,
you can create a `RestDocumentationResultHandler` bean. The auto-configuration will
call `alwaysDo` with this result handler, thereby causing each `MockMvc` call to
automatically generate the default snippets:
[source,java,indent=0]
----
@TestConfiguration
static class ResultHandlerConfiguration{
@Bean
public RestDocumentationResultHandler restDocumentation() {
return MockMvcRestDocumentation.document("{method-name}");
}
}
----
[[boot-features-testing-spring-boot-applications-with-spock]]
==== Using Spock to test Spring Boot applications
If you wish to use Spock to test a Spring Boot application you should add a dependency