Add support for manually managing the RestDocumentationContext

In 1.0, the reliance on JUnit was more widespread than it should have
been. It should have been isolated to the JUnit TestRule implementation,
RestDocumentation. Unfortunately, RestDocumentation was also an argument
to MockMvcRestDocumentation.documentationConfiguration which made it
impossible to use Spring REST Docs without having JUnit on the
classpath.

This commit introduces JUnitRestDocumentation and
ManualRestDocumentation. The format is a direct replacement for
RestDocumentation which has been reworked to delegate to
JUnitRestDocumentation. The latter allows manual management of the
RestDocumentationContext, primarily for use with TestNG.

A new interface, RestDocumentationContextProvider, has been introduced.
It is implemented by RestDocumentation, JUnitRestDocumentation and
ManualRestDocumentation.
MockMvcRestDocumentation.documentationConfiguration has been overridden
to also accept a RestDocumentationContextProvider. The method that
accepts a RestDocumentation has been deprecated, as has
RestDocumentation itself.

The documentation has been updated to encourage the use of
JUnitRestDocumentation and a sample illustrating the use of Spring REST
Docs with TestNG has been added.

Closes gh-171
This commit is contained in:
Andy Wilkinson
2016-02-08 15:22:15 +00:00
parent 6578f42730
commit 0ef9307481
46 changed files with 932 additions and 108 deletions

View File

@@ -16,7 +16,7 @@
package org.springframework.restdocs.restassured;
import org.springframework.restdocs.RestDocumentation;
import org.springframework.restdocs.RestDocumentationContextProvider;
import org.springframework.restdocs.generate.RestDocumentationGenerator;
import org.springframework.restdocs.operation.preprocess.OperationRequestPreprocessor;
import org.springframework.restdocs.operation.preprocess.OperationResponsePreprocessor;
@@ -104,14 +104,14 @@ public abstract class RestAssuredRestDocumentation {
/**
* Provides access to a {@link RestAssuredRestDocumentationConfigurer} that can be
* used to configure Spring REST Docs using the given {@code restDocumentation}.
* used to configure Spring REST Docs using the given {@code contextProvider}.
*
* @param restDocumentation the REST documentation
* @param contextProvider the context provider
* @return the configurer
*/
public static RestAssuredRestDocumentationConfigurer documentationConfiguration(
RestDocumentation restDocumentation) {
return new RestAssuredRestDocumentationConfigurer(restDocumentation);
RestDocumentationContextProvider contextProvider) {
return new RestAssuredRestDocumentationConfigurer(contextProvider);
}
}

View File

@@ -19,8 +19,8 @@ package org.springframework.restdocs.restassured;
import java.util.HashMap;
import java.util.Map;
import org.springframework.restdocs.RestDocumentation;
import org.springframework.restdocs.RestDocumentationContext;
import org.springframework.restdocs.RestDocumentationContextProvider;
import org.springframework.restdocs.config.RestDocumentationConfigurer;
import com.jayway.restassured.filter.Filter;
@@ -42,10 +42,11 @@ public final class RestAssuredRestDocumentationConfigurer
private final RestAssuredSnippetConfigurer snippetConfigurer = new RestAssuredSnippetConfigurer(
this);
private final RestDocumentation restDocumentation;
private final RestDocumentationContextProvider contextProvider;
RestAssuredRestDocumentationConfigurer(RestDocumentation restDocumentation) {
this.restDocumentation = restDocumentation;
RestAssuredRestDocumentationConfigurer(
RestDocumentationContextProvider contextProvider) {
this.contextProvider = contextProvider;
}
@Override
@@ -56,7 +57,7 @@ public final class RestAssuredRestDocumentationConfigurer
@Override
public Response filter(FilterableRequestSpecification requestSpec,
FilterableResponseSpecification responseSpec, FilterContext filterContext) {
RestDocumentationContext context = this.restDocumentation.beforeOperation();
RestDocumentationContext context = this.contextProvider.beforeOperation();
filterContext.setValue(RestDocumentationContext.class.getName(), context);
Map<String, Object> configuration = new HashMap<>();
filterContext.setValue(RestDocumentationFilter.CONTEXT_KEY_CONFIGURATION,

View File

@@ -22,7 +22,7 @@ import java.util.Map;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.springframework.restdocs.RestDocumentation;
import org.springframework.restdocs.JUnitRestDocumentation;
import org.springframework.restdocs.generate.RestDocumentationGenerator;
import org.springframework.restdocs.snippet.WriterResolver;
import org.springframework.restdocs.templates.TemplateEngine;
@@ -48,7 +48,7 @@ import static org.mockito.Mockito.verify;
public class RestAssuredRestDocumentationConfigurerTests {
@Rule
public final RestDocumentation restDocumentation = new RestDocumentation("build");
public final JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation("build");
private final FilterableRequestSpecification requestSpec = mock(FilterableRequestSpecification.class);

View File

@@ -36,7 +36,7 @@ import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.restdocs.RestDocumentation;
import org.springframework.restdocs.JUnitRestDocumentation;
import org.springframework.restdocs.hypermedia.Link;
import org.springframework.restdocs.restassured.RestAssuredRestDocumentationIntegrationTests.TestApplication;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -88,7 +88,7 @@ import static org.springframework.restdocs.test.SnippetMatchers.snippet;
public class RestAssuredRestDocumentationIntegrationTests {
@Rule
public RestDocumentation restDocumentation = new RestDocumentation(
public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation(
"build/generated-snippets");
@Value("${local.server.port}")