Skip to content

Commit

Permalink
Generate proper default display name for test methods
Browse files Browse the repository at this point in the history
This commit revises the default display generation for test methods so
that the following format is used instead of simply the method name.

 - <name>([comma separated list of simple names for parameter types])

Issue: #153
  • Loading branch information
sbrannen committed May 24, 2016
1 parent 19d7dc0 commit aa81d24
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 28 deletions.
2 changes: 1 addition & 1 deletion documentation/src/test/java/example/TestInfoDemo.java
Expand Up @@ -25,7 +25,7 @@ class TestInfoDemo {
@BeforeEach
void init(TestInfo testInfo) {
String displayName = testInfo.getDisplayName();
assertTrue(displayName.equals("TEST 1") || displayName.equals("test2"));
assertTrue(displayName.equals("TEST 1") || displayName.equals("test2()"));
}

@Test
Expand Down
Expand Up @@ -14,6 +14,8 @@
import static java.util.stream.Collectors.joining;
import static org.junit.gen5.commons.meta.API.Usage.Internal;

import java.util.function.Function;

import org.junit.gen5.commons.meta.API;

/**
Expand Down Expand Up @@ -62,19 +64,36 @@ public static boolean isNotBlank(String str) {
}

/**
* Generate a comma-separated list of class names for the supplied
* classes.
* Generate a comma-separated list of fully qualified class names for the
* supplied classes.
*
* @param classes the classes whose names should be included in the
* generated string
* @return a comma-separated list of class names, or an empty string if
* the supplied class array is {@code null} or empty
* @return a comma-separated list of fully qualified class names, or an empty
* string if the supplied class array is {@code null} or empty
* @see #nullSafeToString(Function, Class...)
*/
public static String nullSafeToString(Class<?>... classes) {
return nullSafeToString(Class::getName, classes);
}

/**
* Generate a comma-separated list of mapped values for the supplied classes.
*
* <p>The values are generated by the supplied {@code mapper}
* (e.g., {@code Class::getName}, {@code Class::getSimpleName}, etc.).
*
* @param mapper the mapper to use
* @param classes the classes to map
* @return a comma-separated list of mapped values, or an empty string if
* the supplied class array is {@code null} or empty
* @see #nullSafeToString(Class...)
*/
public static String nullSafeToString(Function<? super Class<?>, ? extends String> mapper, Class<?>... classes) {
if (classes == null || classes.length == 0) {
return "";
}
return stream(classes).map(Class::getName).collect(joining(", "));
return stream(classes).map(mapper).collect(joining(", "));
}

}
Expand Up @@ -45,7 +45,7 @@ public void constructFromMethod() throws Exception {

assertEquals(UniqueId.root("method", "a method id"), descriptor.getUniqueId());
assertEquals(testMethod, descriptor.getTestMethod());
assertEquals("test", descriptor.getDisplayName(), "display name:");
assertEquals("test()", descriptor.getDisplayName(), "display name:");
}

@Test
Expand Down Expand Up @@ -98,7 +98,7 @@ public void constructFromMethodWithParameters() throws Exception {
ASampleTestCase.class, testMethod);

assertEquals(testMethod, descriptor.getTestMethod());
assertEquals("test", descriptor.getDisplayName(), "display name:");
assertEquals("test(String, BigDecimal)", descriptor.getDisplayName(), "display name:");
}

@Tag("classTag1")
Expand Down
Expand Up @@ -10,12 +10,12 @@

package org.junit.gen5.engine.junit5.extension;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.gen5.api.Assertions.assertEquals;
import static org.junit.gen5.api.Assertions.assertTrue;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.List;

import org.junit.gen5.api.AfterAll;
import org.junit.gen5.api.AfterEach;
Expand All @@ -27,17 +27,19 @@
import org.junit.gen5.api.TestInfo;

/**
* Integration tests for {@link TestInfoParameterResolver}
* Integration tests for {@link TestInfoParameterResolver}.
*
* @since 5.0
*/
@Tag("class-tag")
class TestInfoParameterResolverTests {

private Set<String> allDisplayNames = new HashSet<>(
Arrays.asList(new String[] { "getName", "defaultDisplayName", "myName", "getTags" }));
private static List<String> allDisplayNames = Arrays.asList("defaultDisplayName(TestInfo)", "myName",
"getTags(TestInfo)");

@Test
void defaultDisplayName(TestInfo testInfo) {
assertEquals("defaultDisplayName", testInfo.getDisplayName());
assertEquals("defaultDisplayName(TestInfo)", testInfo.getDisplayName());
}

@Test
Expand All @@ -55,13 +57,9 @@ void getTags(TestInfo testInfo) {
}

@BeforeEach
void before(TestInfo testInfo) {
assertTrue(allDisplayNames.contains(testInfo.getDisplayName()));
}

@AfterEach
void after(TestInfo testInfo) {
assertTrue(allDisplayNames.contains(testInfo.getDisplayName()));
void beforeAndAfter(TestInfo testInfo) {
assertThat(allDisplayNames).contains(testInfo.getDisplayName());
}

@BeforeAll
Expand Down
Expand Up @@ -74,7 +74,7 @@ public ClassTestDescriptor(UniqueId uniqueId, Class<?> testClass) {
super(uniqueId);

this.testClass = Preconditions.notNull(testClass, "Class must not be null");
this.displayName = determineDisplayName(testClass, testClass.getName());
this.displayName = determineDisplayName(testClass, () -> testClass.getName());

this.beforeAllMethods = findBeforeAllMethods(testClass);
this.afterAllMethods = findAfterAllMethods(testClass);
Expand Down
Expand Up @@ -21,6 +21,7 @@
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Supplier;

import org.junit.gen5.api.DisplayName;
import org.junit.gen5.api.Executable;
Expand Down Expand Up @@ -55,12 +56,12 @@ protected Set<TestTag> getTags(AnnotatedElement element) {
// @formatter:on
}

protected String determineDisplayName(AnnotatedElement element, String defaultName) {
protected String determineDisplayName(AnnotatedElement element, Supplier<String> defaultNameGenerator) {
// @formatter:off
return findAnnotation(element, DisplayName.class)
.map(DisplayName::value)
.filter(StringUtils::isNotBlank)
.orElse(defaultName);
.orElse(defaultNameGenerator.get());
// @formatter:on
}

Expand Down
Expand Up @@ -29,6 +29,7 @@
import org.junit.gen5.commons.meta.API;
import org.junit.gen5.commons.util.ExceptionUtils;
import org.junit.gen5.commons.util.Preconditions;
import org.junit.gen5.commons.util.StringUtils;
import org.junit.gen5.engine.TestDescriptor;
import org.junit.gen5.engine.TestTag;
import org.junit.gen5.engine.UniqueId;
Expand Down Expand Up @@ -63,11 +64,7 @@ public MethodTestDescriptor(UniqueId uniqueId, Class<?> testClass, Method testMe

this.testClass = Preconditions.notNull(testClass, "Class must not be null");
this.testMethod = Preconditions.notNull(testMethod, "Method must not be null");

// TODO Generate proper default display name.
// String.format("%s#%s(%s)", getTestClass().getName(), this.testMethod.getName(),
// StringUtils.nullSafeToString(this.testMethod.getParameterTypes()));
this.displayName = determineDisplayName(testMethod, testMethod.getName());
this.displayName = determineDisplayName(testMethod, () -> generateDefaultDisplayName(testMethod));

setSource(new JavaMethodSource(testMethod));
}
Expand Down Expand Up @@ -220,4 +217,9 @@ private void invokeAfterEachCallbacks(ExtensionRegistry registry, TestExtensionC
.forEach(extension -> throwableCollector.execute(() -> extension.afterEach(context)));
}

private static String generateDefaultDisplayName(Method method) {
return String.format("%s(%s)", method.getName(),
StringUtils.nullSafeToString(Class::getSimpleName, method.getParameterTypes()));
}

}

0 comments on commit aa81d24

Please sign in to comment.