Skip to content

Commit 794ce24

Browse files
committed
Fall back to display name if method name is blank
Prior to this commit, `VintageTestDescriptor` used the `Description`'s method name when it wasn't null. However, there are edge cases where it can be empty which lead to blank display names which are not allowed by the Platform. Thus, it is now only used if not blank. Fixes #2248.
1 parent b0f54b1 commit 794ce24

File tree

6 files changed

+58
-15
lines changed

6 files changed

+58
-15
lines changed

junit-vintage-engine/src/main/java/org/junit/vintage/engine/descriptor/VintageTestDescriptor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ public VintageTestDescriptor(UniqueId uniqueId, Description description, TestSou
5555
}
5656

5757
private static String generateDisplayName(Description description) {
58-
return description.getMethodName() != null ? description.getMethodName() : description.getDisplayName();
58+
String methodName = description.getMethodName();
59+
return isNotBlank(methodName) ? methodName : description.getDisplayName();
5960
}
6061

6162
public Description getDescription() {

junit-vintage-engine/src/test/java/org/junit/vintage/engine/VintageTestEngineDiscoveryTests.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@
5858
import org.junit.vintage.engine.samples.junit4.Categories.SkippedWithReason;
5959
import org.junit.vintage.engine.samples.junit4.EmptyIgnoredTestCase;
6060
import org.junit.vintage.engine.samples.junit4.IgnoredJUnit4TestCase;
61+
import org.junit.vintage.engine.samples.junit4.JUnit4SuiteWithJUnit4TestCaseWithRunnerWithCustomUniqueIdsAndDisplayNames;
6162
import org.junit.vintage.engine.samples.junit4.JUnit4SuiteWithPlainJUnit4TestCaseWithSingleTestWhichIsIgnored;
6263
import org.junit.vintage.engine.samples.junit4.JUnit4SuiteWithTwoTestCases;
6364
import org.junit.vintage.engine.samples.junit4.JUnit4TestCaseWithDistinguishableOverloadedMethod;
6465
import org.junit.vintage.engine.samples.junit4.JUnit4TestCaseWithIndistinguishableOverloadedMethod;
6566
import org.junit.vintage.engine.samples.junit4.JUnit4TestCaseWithNotFilterableRunner;
66-
import org.junit.vintage.engine.samples.junit4.JUnit4TestCaseWithRunnerWithCustomUniqueIds;
6767
import org.junit.vintage.engine.samples.junit4.ParameterizedTestCase;
6868
import org.junit.vintage.engine.samples.junit4.PlainJUnit4TestCaseWithFiveTestMethods;
6969
import org.junit.vintage.engine.samples.junit4.PlainJUnit4TestCaseWithSingleInheritedTestWhichFails;
@@ -662,19 +662,23 @@ void resolvesClassForMethodSelectorForClassWithNonFilterableRunner() {
662662
}
663663

664664
@Test
665-
void usesCustomUniqueIdsWhenPresent() {
666-
Class<?> testClass = JUnit4TestCaseWithRunnerWithCustomUniqueIds.class;
667-
LauncherDiscoveryRequest request = request().selectors(selectClass(testClass)).build();
665+
void usesCustomUniqueIdsAndDisplayNamesWhenPresent() {
666+
Class<?> suiteClass = JUnit4SuiteWithJUnit4TestCaseWithRunnerWithCustomUniqueIdsAndDisplayNames.class;
667+
LauncherDiscoveryRequest request = request().selectors(selectClass(suiteClass)).build();
668668

669669
TestDescriptor engineDescriptor = discoverTests(request);
670670

671671
TestDescriptor runnerDescriptor = getOnlyElement(engineDescriptor.getChildren());
672-
assertRunnerTestDescriptor(runnerDescriptor, testClass);
672+
assertRunnerTestDescriptor(runnerDescriptor, suiteClass);
673673

674-
TestDescriptor childDescriptor = getOnlyElement(runnerDescriptor.getChildren());
674+
TestDescriptor testClassDescriptor = getOnlyElement(runnerDescriptor.getChildren());
675+
assertEquals("(TestClass)", testClassDescriptor.getDisplayName());
676+
677+
TestDescriptor childDescriptor = getOnlyElement(testClassDescriptor.getChildren());
675678

676-
UniqueId prefix = VintageUniqueIdBuilder.uniqueIdForClass(testClass);
679+
UniqueId prefix = VintageUniqueIdBuilder.uniqueIdForClass(suiteClass);
677680
assertThat(childDescriptor.getUniqueId().toString()).startsWith(prefix.toString());
681+
assertEquals("(TestMethod)", childDescriptor.getDisplayName());
678682

679683
String customUniqueIdValue = childDescriptor.getUniqueId().getSegments().get(2).getType();
680684
assertNotNull(Base64.getDecoder().decode(customUniqueIdValue.getBytes(StandardCharsets.UTF_8)),

junit-vintage-engine/src/test/java/org/junit/vintage/engine/VintageTestEngineExecutionTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
import org.junit.vintage.engine.samples.junit4.JUnit4TestCaseWithExceptionThrowingRunner;
7676
import org.junit.vintage.engine.samples.junit4.JUnit4TestCaseWithFailingDescriptionThatIsNotReportedAsFinished;
7777
import org.junit.vintage.engine.samples.junit4.JUnit4TestCaseWithIndistinguishableOverloadedMethod;
78-
import org.junit.vintage.engine.samples.junit4.JUnit4TestCaseWithRunnerWithCustomUniqueIds;
78+
import org.junit.vintage.engine.samples.junit4.JUnit4TestCaseWithRunnerWithCustomUniqueIdsAndDisplayNames;
7979
import org.junit.vintage.engine.samples.junit4.JUnit4TestCaseWithRunnerWithDuplicateChangingChildDescriptions;
8080
import org.junit.vintage.engine.samples.junit4.MalformedJUnit4TestCase;
8181
import org.junit.vintage.engine.samples.junit4.ParameterizedTestCase;
@@ -610,7 +610,7 @@ void ignoreEventsForUnknownDescriptionsByMisbehavingChildlessRunner() {
610610

611611
@Test
612612
void executesJUnit4TestCaseWithRunnerWithCustomUniqueIds() {
613-
Class<?> testClass = JUnit4TestCaseWithRunnerWithCustomUniqueIds.class;
613+
Class<?> testClass = JUnit4TestCaseWithRunnerWithCustomUniqueIdsAndDisplayNames.class;
614614

615615
execute(testClass).allEvents().assertEventsMatchExactly( //
616616
event(engine(), started()), //
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2015-2020 the original author or authors.
3+
*
4+
* All rights reserved. This program and the accompanying materials are
5+
* made available under the terms of the Eclipse Public License v2.0 which
6+
* accompanies this distribution and is available at
7+
*
8+
* https://www.eclipse.org/legal/epl-v20.html
9+
*/
10+
11+
package org.junit.vintage.engine.samples.junit4;
12+
13+
import org.junit.runner.RunWith;
14+
import org.junit.runners.Suite;
15+
import org.junit.runners.Suite.SuiteClasses;
16+
17+
/**
18+
* @since 5.6.2
19+
*/
20+
@RunWith(Suite.class)
21+
@SuiteClasses(JUnit4TestCaseWithRunnerWithCustomUniqueIdsAndDisplayNames.class)
22+
public class JUnit4SuiteWithJUnit4TestCaseWithRunnerWithCustomUniqueIdsAndDisplayNames {
23+
}
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,18 @@
1212

1313
import org.junit.Assert;
1414
import org.junit.Test;
15+
import org.junit.jupiter.api.DisplayName;
1516
import org.junit.runner.RunWith;
1617

1718
/**
1819
* @since 4.12
1920
*/
20-
@RunWith(RunnerWithCustomUniqueIds.class)
21-
public class JUnit4TestCaseWithRunnerWithCustomUniqueIds {
21+
@DisplayName("(TestClass)")
22+
@RunWith(RunnerWithCustomUniqueIdsAndDisplayNames.class)
23+
public class JUnit4TestCaseWithRunnerWithCustomUniqueIdsAndDisplayNames {
2224

2325
@Test
26+
@DisplayName("(TestMethod)")
2427
public void test() {
2528
Assert.fail();
2629
}
Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.io.Serializable;
1616
import java.util.Objects;
1717

18+
import org.junit.jupiter.api.DisplayName;
1819
import org.junit.runner.Description;
1920
import org.junit.runners.BlockJUnit4ClassRunner;
2021
import org.junit.runners.model.FrameworkMethod;
@@ -23,18 +24,29 @@
2324
/**
2425
* @since 4.12
2526
*/
26-
public class RunnerWithCustomUniqueIds extends BlockJUnit4ClassRunner {
27+
public class RunnerWithCustomUniqueIdsAndDisplayNames extends BlockJUnit4ClassRunner {
2728

28-
public RunnerWithCustomUniqueIds(Class<?> klass) throws InitializationError {
29+
public RunnerWithCustomUniqueIdsAndDisplayNames(Class<?> klass) throws InitializationError {
2930
super(klass);
3031
}
3132

33+
@Override
34+
protected String getName() {
35+
DisplayName displayName = getTestClass().getAnnotation(DisplayName.class);
36+
return displayName == null ? super.getName() : displayName.value();
37+
}
38+
3239
@Override
3340
protected Description describeChild(FrameworkMethod method) {
34-
String testName = testName(method);
41+
String testName = getTestName(method);
3542
return createTestDescription(getTestClass().getJavaClass().getName(), testName, new CustomUniqueId(testName));
3643
}
3744

45+
private String getTestName(FrameworkMethod method) {
46+
DisplayName displayName = method.getAnnotation(DisplayName.class);
47+
return displayName == null ? testName(method) : displayName.value();
48+
}
49+
3850
private static class CustomUniqueId implements Serializable {
3951

4052
private static final long serialVersionUID = 1L;

0 commit comments

Comments
 (0)