Skip to content

Commit bc66fe5

Browse files
committed
Polish "Allow custom snippets directory via JUnit 5"
See gh-633
1 parent 43ec46f commit bc66fe5

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -397,19 +397,19 @@ 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:
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:
402403

403404
[source,java,indent=0]
404405
----
405406
public class JUnit5ExampleTests {
406-
@RegisterExtension
407-
static final RestDocumentationExtension restDocumentation =
408-
new RestDocumentationExtension ("custom");
409-
}
410-
----
411407
408+
@RegisterExtension
409+
final RestDocumentationExtension restDocumentation = new RestDocumentationExtension ("custom");
412410
411+
}
412+
----
413413

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

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

+18-6
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,21 @@ public class RestDocumentationExtension implements BeforeEachCallback, AfterEach
3434

3535
private final String outputDirectory;
3636

37-
public RestDocumentationExtension () {
37+
/**
38+
* Creates a new {@code RestDocumentationExtension} that will use the default output
39+
* directory.
40+
*/
41+
public RestDocumentationExtension() {
3842
this(null);
3943
}
4044

41-
public RestDocumentationExtension (String outputDirectory) {
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) {
4252
this.outputDirectory = outputDirectory;
4353
}
4454

@@ -75,11 +85,13 @@ private ManualRestDocumentation getDelegate(ExtensionContext context) {
7585
this::createManualRestDocumentation, ManualRestDocumentation.class);
7686
}
7787

78-
private ManualRestDocumentation createManualRestDocumentation (Class<ManualRestDocumentation> key) {
79-
if (outputDirectory != null) {
80-
return new ManualRestDocumentation(outputDirectory);
81-
} else {
88+
private ManualRestDocumentation createManualRestDocumentation(Class<ManualRestDocumentation> key) {
89+
if (this.outputDirectory != null) {
90+
return new ManualRestDocumentation(this.outputDirectory);
91+
}
92+
else {
8293
return new ManualRestDocumentation();
8394
}
8495
}
96+
8597
}

0 commit comments

Comments
 (0)