Skip to content

Allow custom snippets directory via JUnit 5 #633

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/src/docs/asciidoc/getting-started.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@
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(),
Expand Down Expand Up @@ -68,8 +78,15 @@ private boolean isTestMethodContext(ExtensionContext context) {
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();
}
}
}