Skip to content

Commit

Permalink
User-defined display name generator "extension"
Browse files Browse the repository at this point in the history
  • Loading branch information
sormuras committed Sep 17, 2018
1 parent 43fe669 commit 7542436
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,29 @@

package example;

import java.lang.reflect.Method;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.DisplayNameGenerator;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

@DisplayName("Leap Year Specification")
class LeapYearSpecDemo {
class DisplayNameGeneratorDemo {

static class Generator implements DisplayNameGenerator {

@Override
public String generateDisplayNameForMethod(Method testMethod) {
return testMethod.getName().toUpperCase();
}
}

@Nested
@ExtendWith(DisplayNameGeneratorDemo.Generator.class)
class A_year_is_a_leap_year {

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public class ClassTestDescriptor extends JupiterTestDescriptor {
private List<Method> afterAllMethods;

public ClassTestDescriptor(UniqueId uniqueId, Class<?> testClass, ConfigurationParameters configurationParameters) {
this(uniqueId, testClass, () -> displayNameGenerator.generateDisplayNameForClass(testClass),
this(uniqueId, testClass, () -> getDisplayNameGenerator(testClass).generateDisplayNameForClass(testClass),
configurationParameters);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.DisplayNameGenerator;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.function.Executable;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ResourceAccessMode;
Expand All @@ -40,6 +41,7 @@
import org.junit.platform.commons.logging.LoggerFactory;
import org.junit.platform.commons.util.ExceptionUtils;
import org.junit.platform.commons.util.Preconditions;
import org.junit.platform.commons.util.ReflectionUtils;
import org.junit.platform.commons.util.StringUtils;
import org.junit.platform.engine.TestDescriptor;
import org.junit.platform.engine.TestSource;
Expand All @@ -61,7 +63,7 @@ public abstract class JupiterTestDescriptor extends AbstractTestDescriptor

private static final ConditionEvaluator conditionEvaluator = new ConditionEvaluator();

static final DisplayNameGenerator displayNameGenerator = new DefaultDisplayNameGenerator();
static final DisplayNameGenerator defaultDisplayNameGenerator = new DefaultDisplayNameGenerator();

JupiterTestDescriptor(UniqueId uniqueId, AnnotatedElement element, Supplier<String> displayNameSupplier,
TestSource source) {
Expand Down Expand Up @@ -97,6 +99,20 @@ protected static Set<TestTag> getTags(AnnotatedElement element) {
// @formatter:on
}

protected static DisplayNameGenerator getDisplayNameGenerator(Class<?> testClass) {
Preconditions.notNull(testClass, "Test class must not be null");
Optional<ExtendWith> optionalExtendWith = findAnnotation(testClass, ExtendWith.class);
if (optionalExtendWith.isPresent()) {
for (Class<?> candidate : optionalExtendWith.get().value()) {
if (!DisplayNameGenerator.class.isAssignableFrom(candidate)) {
continue;
}
return (DisplayNameGenerator) ReflectionUtils.newInstance(candidate);
}
}
return defaultDisplayNameGenerator;
}

protected static String determineDisplayName(AnnotatedElement element, Supplier<String> displayNameSupplier) {
Preconditions.notNull(element, "Annotated element must not be null");
Optional<DisplayName> displayNameAnnotation = findAnnotation(element, DisplayName.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ abstract class MethodBasedTestDescriptor extends JupiterTestDescriptor {

MethodBasedTestDescriptor(UniqueId uniqueId, Class<?> testClass, Method testMethod) {
this(uniqueId,
determineDisplayName(testMethod, () -> displayNameGenerator.generateDisplayNameForMethod(testMethod)),
determineDisplayName(testMethod,
() -> getDisplayNameGenerator(testClass).generateDisplayNameForMethod(testMethod)),
testClass, testMethod);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public class NestedClassTestDescriptor extends ClassTestDescriptor {

public NestedClassTestDescriptor(UniqueId uniqueId, Class<?> testClass,
ConfigurationParameters configurationParameters) {
super(uniqueId, testClass, () -> displayNameGenerator.generateDisplayNameForNestedClass(testClass),
super(uniqueId, testClass,
() -> getDisplayNameGenerator(testClass).generateDisplayNameForNestedClass(testClass),
configurationParameters);

this.tags = getTags(testClass);
Expand Down

0 comments on commit 7542436

Please sign in to comment.