Skip to content

Commit ce850a5

Browse files
committed
Support overloaded test methods in CompileWithTargetClassAccessExtension
Prior to this commit, CompileWithTargetClassAccessExtension failed to properly select overloaded test methods because it ignored the method parameter list when looking up the test method. This commit addresses this issue by selecting the test method using its fully qualified method name which takes in account the class name, method name, and parameter names. Closes gh-28901
1 parent 8fb27c3 commit ce850a5

File tree

1 file changed

+4
-22
lines changed

1 file changed

+4
-22
lines changed

spring-core-test/src/main/java/org/springframework/aot/test/generator/compile/CompileWithTargetClassAccessExtension.java

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@
2828
import org.junit.platform.launcher.listeners.SummaryGeneratingListener;
2929
import org.junit.platform.launcher.listeners.TestExecutionSummary;
3030

31-
import org.springframework.util.Assert;
32-
import org.springframework.util.ReflectionUtils;
33-
31+
import static org.junit.platform.commons.util.ReflectionUtils.getFullyQualifiedMethodName;
3432
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectMethod;
3533
import static org.junit.platform.launcher.EngineFilter.includeEngines;
3634

@@ -121,20 +119,18 @@ private void runTestWithModifiedClassPath(
121119
testClass.getClassLoader());
122120
Thread.currentThread().setContextClassLoader(forkedClassPathClassLoader);
123121
try {
124-
runTest(forkedClassPathClassLoader, testClass.getName(), testMethod.getName());
122+
runTest(forkedClassPathClassLoader, testClass, testMethod);
125123
}
126124
finally {
127125
Thread.currentThread().setContextClassLoader(originalClassLoader);
128126
}
129127
}
130128

131-
private void runTest(ClassLoader classLoader, String testClassName, String testMethodName)
129+
private void runTest(ClassLoader classLoader, Class<?> testClass, Method testMethod)
132130
throws Throwable {
133131

134-
Class<?> testClass = classLoader.loadClass(testClassName);
135-
Method testMethod = findMethod(testClass, testMethodName);
136132
LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
137-
.selectors(selectMethod(testClass, testMethod))
133+
.selectors(selectMethod(getFullyQualifiedMethodName(testClass, testMethod)))
138134
.filters(includeEngines("junit-jupiter"))
139135
.build();
140136
SummaryGeneratingListener listener = new SummaryGeneratingListener();
@@ -146,20 +142,6 @@ private void runTest(ClassLoader classLoader, String testClassName, String testM
146142
}
147143
}
148144

149-
private Method findMethod(Class<?> testClass, String testMethodName) {
150-
Method method = ReflectionUtils.findMethod(testClass, testMethodName);
151-
if (method == null) {
152-
Method[] methods = ReflectionUtils.getUniqueDeclaredMethods(testClass);
153-
for (Method candidate : methods) {
154-
if (candidate.getName().equals(testMethodName)) {
155-
return candidate;
156-
}
157-
}
158-
}
159-
Assert.state(method != null, () -> "Unable to find " + testClass + "." + testMethodName);
160-
return method;
161-
}
162-
163145

164146
@FunctionalInterface
165147
interface Action {

0 commit comments

Comments
 (0)