Skip to content

Commit 43ec46f

Browse files
quandorwilkinsona
authored andcommitted
Allow custom snippets directory via JUnit 5
See gh-633
1 parent cc22dbf commit 43ec46f

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

docs/src/docs/asciidoc/getting-started.adoc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,18 @@ based on your project's build tool:
397397

398398
|===
399399

400+
If you are using JUnit 5.1 or later the default can be overridden by providing
401+
an output directory when registering the `RestDocumentationExtension` instance:
402+
403+
[source,java,indent=0]
404+
----
405+
public class JUnit5ExampleTests {
406+
@RegisterExtension
407+
static final RestDocumentationExtension restDocumentation =
408+
new RestDocumentationExtension ("custom");
409+
}
410+
----
411+
400412

401413

402414
Next, you must provide a `@BeforeEach` method to configure MockMvc, WebTestClient, or

spring-restdocs-core/src/main/java/org/springframework/restdocs/RestDocumentationExtension.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@
3232
*/
3333
public class RestDocumentationExtension implements BeforeEachCallback, AfterEachCallback, ParameterResolver {
3434

35+
private final String outputDirectory;
36+
37+
public RestDocumentationExtension () {
38+
this(null);
39+
}
40+
41+
public RestDocumentationExtension (String outputDirectory) {
42+
this.outputDirectory = outputDirectory;
43+
}
44+
3545
@Override
3646
public void beforeEach(ExtensionContext context) throws Exception {
3747
this.getDelegate(context).beforeTest(context.getRequiredTestClass(), context.getRequiredTestMethod().getName());
@@ -62,7 +72,14 @@ private boolean isTestMethodContext(ExtensionContext context) {
6272
private ManualRestDocumentation getDelegate(ExtensionContext context) {
6373
Namespace namespace = Namespace.create(getClass(), context.getUniqueId());
6474
return context.getStore(namespace).getOrComputeIfAbsent(ManualRestDocumentation.class,
65-
(key) -> new ManualRestDocumentation(), ManualRestDocumentation.class);
75+
this::createManualRestDocumentation, ManualRestDocumentation.class);
6676
}
6777

78+
private ManualRestDocumentation createManualRestDocumentation (Class<ManualRestDocumentation> key) {
79+
if (outputDirectory != null) {
80+
return new ManualRestDocumentation(outputDirectory);
81+
} else {
82+
return new ManualRestDocumentation();
83+
}
84+
}
6885
}

0 commit comments

Comments
 (0)