Merge pull request #633 from nt-jj
* gh-633: Polish "Allow custom snippets directory via JUnit 5" Allow custom snippets directory via JUnit 5 Closes gh-633
This commit is contained in:
@@ -397,7 +397,19 @@ based on your project's build tool:
|
||||
|
||||
|===
|
||||
|
||||
If you are using JUnit 5.1, you can override the default by registering the extension
|
||||
as a field in your test class and providing an output directory when creating it. The
|
||||
following example shows how to do so:
|
||||
|
||||
[source,java,indent=0]
|
||||
----
|
||||
public class JUnit5ExampleTests {
|
||||
|
||||
@RegisterExtension
|
||||
final RestDocumentationExtension restDocumentation = new RestDocumentationExtension ("custom");
|
||||
|
||||
}
|
||||
----
|
||||
|
||||
Next, you must provide a `@BeforeEach` method to configure MockMvc, WebTestClient, or
|
||||
REST Assured. The following listings show how to do so:
|
||||
|
||||
@@ -32,6 +32,26 @@ import org.junit.jupiter.api.extension.ParameterResolver;
|
||||
*/
|
||||
public class RestDocumentationExtension implements BeforeEachCallback, AfterEachCallback, ParameterResolver {
|
||||
|
||||
private final String outputDirectory;
|
||||
|
||||
/**
|
||||
* Creates a new {@code RestDocumentationExtension} that will use the default output
|
||||
* directory.
|
||||
*/
|
||||
public RestDocumentationExtension() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@code RestDocumentationExtension} that will use the given
|
||||
* {@code outputDirectory}.
|
||||
* @param outputDirectory snippet output directory
|
||||
* @since 2.0.4
|
||||
*/
|
||||
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 +82,16 @@ 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 (this.outputDirectory != null) {
|
||||
return new ManualRestDocumentation(this.outputDirectory);
|
||||
}
|
||||
else {
|
||||
return new ManualRestDocumentation();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user