Skip to content

Commit 0e9f9fd

Browse files
committed
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
2 parents cc22dbf + bc66fe5 commit 0e9f9fd

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-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,7 +397,19 @@ based on your project's build tool:
397397

398398
|===
399399

400+
If you are using JUnit 5.1, you can override the default by registering the extension
401+
as a field in your test class and providing an output directory when creating it. The
402+
following example shows how to do so:
400403

404+
[source,java,indent=0]
405+
----
406+
public class JUnit5ExampleTests {
407+
408+
@RegisterExtension
409+
final RestDocumentationExtension restDocumentation = new RestDocumentationExtension ("custom");
410+
411+
}
412+
----
401413

402414
Next, you must provide a `@BeforeEach` method to configure MockMvc, WebTestClient, or
403415
REST Assured. The following listings show how to do so:

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

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

35+
private final String outputDirectory;
36+
37+
/**
38+
* Creates a new {@code RestDocumentationExtension} that will use the default output
39+
* directory.
40+
*/
41+
public RestDocumentationExtension() {
42+
this(null);
43+
}
44+
45+
/**
46+
* Creates a new {@code RestDocumentationExtension} that will use the given
47+
* {@code outputDirectory}.
48+
* @param outputDirectory snippet output directory
49+
* @since 2.0.4
50+
*/
51+
public RestDocumentationExtension(String outputDirectory) {
52+
this.outputDirectory = outputDirectory;
53+
}
54+
3555
@Override
3656
public void beforeEach(ExtensionContext context) throws Exception {
3757
this.getDelegate(context).beforeTest(context.getRequiredTestClass(), context.getRequiredTestMethod().getName());
@@ -62,7 +82,16 @@ private boolean isTestMethodContext(ExtensionContext context) {
6282
private ManualRestDocumentation getDelegate(ExtensionContext context) {
6383
Namespace namespace = Namespace.create(getClass(), context.getUniqueId());
6484
return context.getStore(namespace).getOrComputeIfAbsent(ManualRestDocumentation.class,
65-
(key) -> new ManualRestDocumentation(), ManualRestDocumentation.class);
85+
this::createManualRestDocumentation, ManualRestDocumentation.class);
86+
}
87+
88+
private ManualRestDocumentation createManualRestDocumentation(Class<ManualRestDocumentation> key) {
89+
if (this.outputDirectory != null) {
90+
return new ManualRestDocumentation(this.outputDirectory);
91+
}
92+
else {
93+
return new ManualRestDocumentation();
94+
}
6695
}
6796

6897
}

0 commit comments

Comments
 (0)