Skip to content

Commit 2791365

Browse files
committed
Log additional information about partially resolved Unique IDs
Prior to this commit, if a selected Unique ID could not be fully resolved in JUnit Jupiter, the JavaElementsResolver logged a warning stating that the Unique ID could not be resolved at all. This was, however, extremely misleading if the Unique ID was in fact partially resolved. This commit addresses this issue by providing additional information in the log message if a Unique ID can only be partially resolved. Issue: #1031
1 parent b9813b7 commit 2791365

File tree

1 file changed

+32
-13
lines changed

1 file changed

+32
-13
lines changed

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/discovery/JavaElementsResolver.java

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.junit.platform.commons.util.ReflectionUtils;
3535
import org.junit.platform.engine.TestDescriptor;
3636
import org.junit.platform.engine.UniqueId;
37+
import org.junit.platform.engine.UniqueId.Segment;
3738

3839
/**
3940
* @since 5.0
@@ -57,7 +58,7 @@ void resolveClass(Class<?> testClass) {
5758
resolvedDescriptors.forEach(this::resolveChildren);
5859

5960
if (resolvedDescriptors.isEmpty()) {
60-
logger.warning(() -> format("Class '%s' could not be resolved", testClass.getName()));
61+
logger.warning(() -> format("Class '%s' could not be resolved.", testClass.getName()));
6162
}
6263
}
6364

@@ -66,7 +67,7 @@ void resolveMethod(Class<?> testClass, Method testMethod) {
6667
Set<TestDescriptor> resolvedDescriptors = resolveForAllParents(testMethod, potentialParents);
6768

6869
if (resolvedDescriptors.isEmpty()) {
69-
logger.warning(() -> format("Method '%s' could not be resolved", testMethod.toGenericString()));
70+
logger.warning(() -> format("Method '%s' could not be resolved.", testMethod.toGenericString()));
7071
}
7172

7273
logMultipleTestDescriptorsForSingleElement(testMethod, resolvedDescriptors);
@@ -87,29 +88,45 @@ void resolveUniqueId(UniqueId uniqueId) {
8788

8889
// Ignore Unique IDs from other test engines.
8990
if (JupiterTestEngine.ENGINE_ID.equals(engineId)) {
90-
List<UniqueId.Segment> segments = new ArrayList<>(uniqueId.getSegments());
91+
List<Segment> remainingSegments = new ArrayList<>(uniqueId.getSegments());
9192

9293
// Ignore engine ID
93-
segments.remove(0);
94+
remainingSegments.remove(0);
9495

95-
if (!resolveUniqueId(this.engineDescriptor, segments)) {
96-
logger.warning(() -> format("Unique ID '%s' could not be resolved", uniqueId));
96+
int numSegmentsToResolve = remainingSegments.size();
97+
int numSegmentsResolved = resolveUniqueId(this.engineDescriptor, remainingSegments);
98+
99+
if (numSegmentsResolved == 0) {
100+
logger.warning(() -> format("Unique ID '%s' could not be resolved.", uniqueId));
101+
}
102+
else if (numSegmentsResolved != numSegmentsToResolve) {
103+
logger.warning(() -> {
104+
List<Segment> segments = uniqueId.getSegments();
105+
List<Segment> unresolved = segments.subList(1, segments.size()); // Remove engine ID
106+
unresolved = unresolved.subList(numSegmentsResolved, unresolved.size()); // Remove resolved segments
107+
return format("Unique ID '%s' could only be partially resolved. "
108+
+ "All resolved segments will be executed; however, the "
109+
+ "following segments could not be resolved: %s",
110+
uniqueId, unresolved);
111+
});
97112
}
98113
}
99114
});
100115
}
101116

102117
/**
103-
* Return true if all segments of unique ID could be resolved
118+
* Attempt to resolve all segments for the supplied unique ID.
119+
*
120+
* @return the number of segments resolved
104121
*/
105-
private boolean resolveUniqueId(TestDescriptor parent, List<UniqueId.Segment> remainingSegments) {
122+
private int resolveUniqueId(TestDescriptor parent, List<Segment> remainingSegments) {
106123
if (remainingSegments.isEmpty()) {
107124
resolveChildren(parent);
108-
return true;
125+
return 0;
109126
}
110127

111-
UniqueId.Segment head = remainingSegments.remove(0);
112-
for (ElementResolver resolver : resolvers) {
128+
Segment head = remainingSegments.remove(0);
129+
for (ElementResolver resolver : this.resolvers) {
113130
Optional<TestDescriptor> resolvedDescriptor = resolver.resolveUniqueId(head, parent);
114131
if (!resolvedDescriptor.isPresent()) {
115132
continue;
@@ -122,13 +139,15 @@ private boolean resolveUniqueId(TestDescriptor parent, List<UniqueId.Segment> re
122139
parent.addChild(newDescriptor);
123140
return newDescriptor;
124141
});
125-
return resolveUniqueId(descriptor, remainingSegments);
142+
return 1 + resolveUniqueId(descriptor, remainingSegments);
126143
}
127-
return false;
144+
145+
return 0;
128146
}
129147

130148
private Set<TestDescriptor> resolveContainerWithChildren(Class<?> containerClass,
131149
Set<TestDescriptor> potentialParents) {
150+
132151
Set<TestDescriptor> resolvedDescriptors = resolveForAllParents(containerClass, potentialParents);
133152
resolvedDescriptors.forEach(this::resolveChildren);
134153
return resolvedDescriptors;

0 commit comments

Comments
 (0)