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
This commit is contained in:
parent
8fb27c3857
commit
ce850a583c
|
@ -28,9 +28,7 @@ import org.junit.platform.launcher.core.LauncherFactory;
|
||||||
import org.junit.platform.launcher.listeners.SummaryGeneratingListener;
|
import org.junit.platform.launcher.listeners.SummaryGeneratingListener;
|
||||||
import org.junit.platform.launcher.listeners.TestExecutionSummary;
|
import org.junit.platform.launcher.listeners.TestExecutionSummary;
|
||||||
|
|
||||||
import org.springframework.util.Assert;
|
import static org.junit.platform.commons.util.ReflectionUtils.getFullyQualifiedMethodName;
|
||||||
import org.springframework.util.ReflectionUtils;
|
|
||||||
|
|
||||||
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectMethod;
|
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectMethod;
|
||||||
import static org.junit.platform.launcher.EngineFilter.includeEngines;
|
import static org.junit.platform.launcher.EngineFilter.includeEngines;
|
||||||
|
|
||||||
|
@ -121,20 +119,18 @@ class CompileWithTargetClassAccessExtension implements InvocationInterceptor {
|
||||||
testClass.getClassLoader());
|
testClass.getClassLoader());
|
||||||
Thread.currentThread().setContextClassLoader(forkedClassPathClassLoader);
|
Thread.currentThread().setContextClassLoader(forkedClassPathClassLoader);
|
||||||
try {
|
try {
|
||||||
runTest(forkedClassPathClassLoader, testClass.getName(), testMethod.getName());
|
runTest(forkedClassPathClassLoader, testClass, testMethod);
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
Thread.currentThread().setContextClassLoader(originalClassLoader);
|
Thread.currentThread().setContextClassLoader(originalClassLoader);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void runTest(ClassLoader classLoader, String testClassName, String testMethodName)
|
private void runTest(ClassLoader classLoader, Class<?> testClass, Method testMethod)
|
||||||
throws Throwable {
|
throws Throwable {
|
||||||
|
|
||||||
Class<?> testClass = classLoader.loadClass(testClassName);
|
|
||||||
Method testMethod = findMethod(testClass, testMethodName);
|
|
||||||
LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
|
LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
|
||||||
.selectors(selectMethod(testClass, testMethod))
|
.selectors(selectMethod(getFullyQualifiedMethodName(testClass, testMethod)))
|
||||||
.filters(includeEngines("junit-jupiter"))
|
.filters(includeEngines("junit-jupiter"))
|
||||||
.build();
|
.build();
|
||||||
SummaryGeneratingListener listener = new SummaryGeneratingListener();
|
SummaryGeneratingListener listener = new SummaryGeneratingListener();
|
||||||
|
@ -146,20 +142,6 @@ class CompileWithTargetClassAccessExtension implements InvocationInterceptor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Method findMethod(Class<?> testClass, String testMethodName) {
|
|
||||||
Method method = ReflectionUtils.findMethod(testClass, testMethodName);
|
|
||||||
if (method == null) {
|
|
||||||
Method[] methods = ReflectionUtils.getUniqueDeclaredMethods(testClass);
|
|
||||||
for (Method candidate : methods) {
|
|
||||||
if (candidate.getName().equals(testMethodName)) {
|
|
||||||
return candidate;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Assert.state(method != null, () -> "Unable to find " + testClass + "." + testMethodName);
|
|
||||||
return method;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@FunctionalInterface
|
@FunctionalInterface
|
||||||
interface Action {
|
interface Action {
|
||||||
|
|
Loading…
Reference in New Issue