Allow custom snippets directory via JUnit 5
See gh-633
This commit is contained in:
committed by
Andy Wilkinson
parent
cc22dbfe26
commit
43ec46fcc1
@@ -397,6 +397,18 @@ based on your project's build tool:
|
||||
|
||||
|===
|
||||
|
||||
If you are using JUnit 5.1 or later the default can be overridden by providing
|
||||
an output directory when registering the `RestDocumentationExtension` instance:
|
||||
|
||||
[source,java,indent=0]
|
||||
----
|
||||
public class JUnit5ExampleTests {
|
||||
@RegisterExtension
|
||||
static final RestDocumentationExtension restDocumentation =
|
||||
new RestDocumentationExtension ("custom");
|
||||
}
|
||||
----
|
||||
|
||||
|
||||
|
||||
Next, you must provide a `@BeforeEach` method to configure MockMvc, WebTestClient, or
|
||||
|
||||
@@ -32,6 +32,16 @@ import org.junit.jupiter.api.extension.ParameterResolver;
|
||||
*/
|
||||
public class RestDocumentationExtension implements BeforeEachCallback, AfterEachCallback, ParameterResolver {
|
||||
|
||||
private final String outputDirectory;
|
||||
|
||||
public RestDocumentationExtension () {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public RestDocumentationExtension (String outputDirectory) {
|
||||
this.outputDirectory = outputDirectory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeEach(ExtensionContext context) throws Exception {
|
||||
this.getDelegate(context).beforeTest(context.getRequiredTestClass(), context.getRequiredTestMethod().getName());
|
||||
@@ -62,7 +72,14 @@ public class RestDocumentationExtension implements BeforeEachCallback, AfterEach
|
||||
private ManualRestDocumentation getDelegate(ExtensionContext context) {
|
||||
Namespace namespace = Namespace.create(getClass(), context.getUniqueId());
|
||||
return context.getStore(namespace).getOrComputeIfAbsent(ManualRestDocumentation.class,
|
||||
(key) -> new ManualRestDocumentation(), ManualRestDocumentation.class);
|
||||
this::createManualRestDocumentation, ManualRestDocumentation.class);
|
||||
}
|
||||
|
||||
private ManualRestDocumentation createManualRestDocumentation (Class<ManualRestDocumentation> key) {
|
||||
if (outputDirectory != null) {
|
||||
return new ManualRestDocumentation(outputDirectory);
|
||||
} else {
|
||||
return new ManualRestDocumentation();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user