Fix ModifiedClassPathExtension with parameterized tests
Closes gh-33014
This commit is contained in:
parent
d2cceb6b77
commit
ac6ad7c0f1
|
@ -32,9 +32,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 org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
import org.springframework.util.ReflectionUtils;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A custom {@link Extension} that runs tests using a modified class path. Entries are
|
* A custom {@link Extension} that runs tests using a modified class path. Entries are
|
||||||
|
@ -102,18 +100,16 @@ class ModifiedClassPathExtension implements InvocationInterceptor {
|
||||||
ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
|
ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
|
||||||
Thread.currentThread().setContextClassLoader(modifiedClassLoader);
|
Thread.currentThread().setContextClassLoader(modifiedClassLoader);
|
||||||
try {
|
try {
|
||||||
runTest(modifiedClassLoader, testClass.getName(), testMethod.getName());
|
runTest(extensionContext.getUniqueId());
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
Thread.currentThread().setContextClassLoader(originalClassLoader);
|
Thread.currentThread().setContextClassLoader(originalClassLoader);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void runTest(ClassLoader classLoader, String testClassName, String testMethodName) throws Throwable {
|
private void runTest(String testId) throws Throwable {
|
||||||
Class<?> testClass = classLoader.loadClass(testClassName);
|
|
||||||
Method testMethod = findMethod(testClass, testMethodName);
|
|
||||||
LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
|
LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
|
||||||
.selectors(DiscoverySelectors.selectMethod(testClass, testMethod)).build();
|
.selectors(DiscoverySelectors.selectUniqueId(testId)).build();
|
||||||
Launcher launcher = LauncherFactory.create();
|
Launcher launcher = LauncherFactory.create();
|
||||||
TestPlan testPlan = launcher.discover(request);
|
TestPlan testPlan = launcher.discover(request);
|
||||||
SummaryGeneratingListener listener = new SummaryGeneratingListener();
|
SummaryGeneratingListener listener = new SummaryGeneratingListener();
|
||||||
|
@ -125,20 +121,6 @@ class ModifiedClassPathExtension 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void intercept(Invocation<Void> invocation, ExtensionContext extensionContext) throws Throwable {
|
private void intercept(Invocation<Void> invocation, ExtensionContext extensionContext) throws Throwable {
|
||||||
if (isModifiedClassPathClassLoader(extensionContext)) {
|
if (isModifiedClassPathClassLoader(extensionContext)) {
|
||||||
invocation.proceed();
|
invocation.proceed();
|
||||||
|
|
|
@ -0,0 +1,52 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2012-2020 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.boot.testsupport.classpath;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.CsvSource;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests for {@link ForkedClassPath @ForkedClassPath}.
|
||||||
|
*
|
||||||
|
* @author Andy Wilkinson
|
||||||
|
*/
|
||||||
|
@ForkedClassPath
|
||||||
|
class ModifiedClassPathExtensionForkParameterizedTests {
|
||||||
|
|
||||||
|
private static final List<String> arguments = new ArrayList<>();
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@CsvSource({ "one", "two", "three" })
|
||||||
|
void testIsInvokedOnceForEachArgument(String argument) {
|
||||||
|
if (argument.equals("one")) {
|
||||||
|
assertThat(arguments).isEmpty();
|
||||||
|
}
|
||||||
|
else if (argument.equals("two")) {
|
||||||
|
assertThat(arguments).doesNotContain("two", "three");
|
||||||
|
}
|
||||||
|
else if (argument.equals("three")) {
|
||||||
|
assertThat(arguments).doesNotContain("three");
|
||||||
|
}
|
||||||
|
arguments.add(argument);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -32,7 +32,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||||
*
|
*
|
||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
*/
|
*/
|
||||||
class ModifiedClassPathExtensionOverridesParametrizedTests {
|
class ModifiedClassPathExtensionOverridesParameterizedTests {
|
||||||
|
|
||||||
@ParameterizedTest
|
@ParameterizedTest
|
||||||
@ForkedClassPath
|
@ForkedClassPath
|
Loading…
Reference in New Issue