Add support for using the class name in parameterised operation name
This commit adds support for three new placeholders that can be used
when specifying the name of an operation: {ClassName}, {class-name},
and {class_name}. This allows the operation name to be configured once
in an abstract superclass and reused across multiple test classes.
Closes gh-116
This commit is contained in:
@@ -386,7 +386,7 @@ are supported:
|
||||
| Parameter | Description
|
||||
|
||||
| {methodName}
|
||||
| The name of the test method, formatted using camelCase
|
||||
| The unmodified name of the test method
|
||||
|
||||
| {method-name}
|
||||
| The name of the test method, formatted using kebab-case
|
||||
@@ -394,15 +394,26 @@ are supported:
|
||||
| {method_name}
|
||||
| The name of the test method, formatted using snake_case
|
||||
|
||||
| {ClassName}
|
||||
| The unmodified name of the test class
|
||||
|
||||
| {class-name}
|
||||
| The name of the test class, formatted using kebab-case
|
||||
|
||||
| {class_name}
|
||||
| The nome of the test class, formatted using snake_case
|
||||
|
||||
| {step}
|
||||
| The count of calls to MockMvc.perform in the current test
|
||||
|===
|
||||
|
||||
For example, `document("{method-name}")` in a test method named `creatingANote` will write
|
||||
snippets into a directory named `creating-a-note`.
|
||||
For example, `document("{class-name}/{method-name}")` in a test method named
|
||||
`creatingANote` on the test class `GettingStartedDocumentaiton`, will write
|
||||
snippets into a directory named `getting-started-documentation/creating-a-note`.
|
||||
|
||||
A parameterized output directory is particularly useful in combination with Spring MVC
|
||||
Test's `alwaysDo` functionality. It allows documentation to be configured once in a setup method:
|
||||
Test's `alwaysDo` functionality. It allows documentation to be configured once in a setup
|
||||
method:
|
||||
|
||||
[source,java,indent=0]
|
||||
----
|
||||
|
||||
@@ -28,15 +28,23 @@ import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;
|
||||
* <ul>
|
||||
* <li>{@code step} – the {@link RestDocumentationContext#getStepCount() step current
|
||||
* count}.
|
||||
* <li>{@code methodName} - the name of the
|
||||
* {@link RestDocumentationContext#getTestMethodName() current test method} formatted
|
||||
* using camelCase
|
||||
* <li>{@code methodName} - the unmodified name of the
|
||||
* {@link RestDocumentationContext#getTestMethodName() current test method} without
|
||||
* applying any formatting
|
||||
* <li>{@code method-name} - the name of the
|
||||
* {@link RestDocumentationContext#getTestMethodName() current test method} formatted
|
||||
* using kebab-case
|
||||
* <li>{@code method_name} - the name of the
|
||||
* {@link RestDocumentationContext#getTestMethodName() current test method} formatted
|
||||
* using snake_case
|
||||
* <li>{@code ClassName} - the unmodified {@link Class#getSimpleName() simple name} of the
|
||||
* {@link RestDocumentationContext#getTestClass() current test class}
|
||||
* <li>{@code class-name} - the {@link Class#getSimpleName() simple name} of the
|
||||
* {@link RestDocumentationContext#getTestClass() current test class} formatted using
|
||||
* kebab-case
|
||||
* <li>{@code class_name} - the {@link Class#getSimpleName() simple name} of the
|
||||
* {@link RestDocumentationContext#getTestClass() current test class} formatted using
|
||||
* snake case
|
||||
* </ul>
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
@@ -71,6 +79,15 @@ public class RestDocumentationContextPlaceholderResolver implements PlaceholderR
|
||||
if ("method_name".equals(placeholderName)) {
|
||||
return camelCaseToSnakeCase(this.context.getTestMethodName());
|
||||
}
|
||||
if ("ClassName".equals(placeholderName)) {
|
||||
return this.context.getTestClass().getSimpleName();
|
||||
}
|
||||
if ("class-name".equals(placeholderName)) {
|
||||
return camelCaseToKebabCase(this.context.getTestClass().getSimpleName());
|
||||
}
|
||||
if ("class_name".equals(placeholderName)) {
|
||||
return camelCaseToSnakeCase(this.context.getTestClass().getSimpleName());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -108,7 +125,9 @@ public class RestDocumentationContextPlaceholderResolver implements PlaceholderR
|
||||
Matcher matcher = CAMEL_CASE_PATTERN.matcher(string);
|
||||
StringBuffer result = new StringBuffer();
|
||||
while (matcher.find()) {
|
||||
matcher.appendReplacement(result, separator + matcher.group(1).toLowerCase());
|
||||
String replacement = (matcher.start() > 0) ? separator
|
||||
+ matcher.group(1).toLowerCase() : matcher.group(1).toLowerCase();
|
||||
matcher.appendReplacement(result, replacement);
|
||||
}
|
||||
matcher.appendTail(result);
|
||||
return result.toString();
|
||||
|
||||
@@ -32,14 +32,14 @@ import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;
|
||||
public class RestDocumentationContextPlaceholderResolverTests {
|
||||
|
||||
@Test
|
||||
public void dashSeparatedMethodName() throws Exception {
|
||||
public void kebabCaseMethodName() throws Exception {
|
||||
assertThat(
|
||||
createResolver("dashSeparatedMethodName").resolvePlaceholder(
|
||||
"method-name"), equalTo("dash-separated-method-name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void underscoreSeparatedMethodName() throws Exception {
|
||||
public void snakeCaseMethodName() throws Exception {
|
||||
assertThat(
|
||||
createResolver("underscoreSeparatedMethodName").resolvePlaceholder(
|
||||
"method_name"), equalTo("underscore_separated_method_name"));
|
||||
@@ -52,14 +52,37 @@ public class RestDocumentationContextPlaceholderResolverTests {
|
||||
equalTo("camelCaseMethodName"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void kebabCaseClassName() throws Exception {
|
||||
assertThat(createResolver().resolvePlaceholder("class-name"),
|
||||
equalTo("rest-documentation-context-placeholder-resolver-tests"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void snakeCaseClassName() throws Exception {
|
||||
assertThat(createResolver().resolvePlaceholder("class_name"),
|
||||
equalTo("rest_documentation_context_placeholder_resolver_tests"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void camelCaseClassName() throws Exception {
|
||||
assertThat(createResolver().resolvePlaceholder("ClassName"),
|
||||
equalTo("RestDocumentationContextPlaceholderResolverTests"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void stepCount() throws Exception {
|
||||
assertThat(createResolver("stepCount").resolvePlaceholder("step"), equalTo("0"));
|
||||
}
|
||||
|
||||
private PlaceholderResolver createResolver() {
|
||||
return new RestDocumentationContextPlaceholderResolver(
|
||||
new RestDocumentationContext(getClass(), null, null));
|
||||
}
|
||||
|
||||
private PlaceholderResolver createResolver(String methodName) {
|
||||
return new RestDocumentationContextPlaceholderResolver(
|
||||
new RestDocumentationContext(null, methodName, null));
|
||||
new RestDocumentationContext(getClass(), methodName, null));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user