Skip to content

Commit 1928f36

Browse files
committed
Introduce Jupiter-specific DeclaredMethodSelector
1 parent c6999dc commit 1928f36

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -323,11 +323,7 @@ private DiscoverySelector selectClass(List<Class<?>> classes) {
323323
}
324324

325325
private DiscoverySelector selectMethod(List<Class<?>> classes, Method method) {
326-
if (classes.size() == 1) {
327-
return DiscoverySelectors.selectMethod(classes.get(0), method);
328-
}
329-
int lastIndex = classes.size() - 1;
330-
return DiscoverySelectors.selectNestedMethod(classes.subList(0, lastIndex), classes.get(lastIndex), method);
326+
return new DeclaredMethodSelector(classes, method);
331327
}
332328

333329
static class DummyClassTemplateInvocationContext implements ClassTemplateInvocationContext {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2015-2025 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.jupiter.engine.discovery;
12+
13+
import java.lang.reflect.Method;
14+
import java.util.List;
15+
16+
import org.junit.platform.commons.util.Preconditions;
17+
import org.junit.platform.engine.DiscoverySelector;
18+
19+
/**
20+
* @since 6.0.1
21+
*/
22+
record DeclaredMethodSelector(List<Class<?>> testClasses, Method method) implements DiscoverySelector {
23+
DeclaredMethodSelector {
24+
Preconditions.notEmpty(testClasses, "testClasses must not be empty");
25+
Preconditions.containsNoNullElements(testClasses, "testClasses must not contain null elements");
26+
Preconditions.notNull(method, "method must not be null");
27+
}
28+
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,20 @@ public Resolution resolve(NestedMethodSelector selector, Context context) {
8383
Match::exact);
8484
}
8585

86+
@Override
87+
public Resolution resolve(DiscoverySelector selector, Context context) {
88+
if (selector instanceof DeclaredMethodSelector methodSelector) {
89+
var testClasses = methodSelector.testClasses();
90+
if (testClasses.size() == 1) {
91+
return resolve(context, emptyList(), testClasses.get(0), methodSelector::method, Match::exact);
92+
}
93+
int lastIndex = testClasses.size() - 1;
94+
return resolve(context, testClasses.subList(0, lastIndex), testClasses.get(lastIndex),
95+
methodSelector::method, Match::exact);
96+
}
97+
return unresolved();
98+
}
99+
86100
private Resolution resolve(Context context, List<Class<?>> enclosingClasses, Class<?> testClass,
87101
Supplier<Method> methodSupplier,
88102
BiFunction<TestDescriptor, Supplier<Set<? extends DiscoverySelector>>, Match> matchFactory) {

0 commit comments

Comments
 (0)