Merge branch '2.6.x' into 2.7.x

See gh-33015
pull/33648/head
Andy Wilkinson 2 years ago
commit 7c0f397129

@ -32,9 +32,7 @@ import org.junit.platform.launcher.core.LauncherFactory;
import org.junit.platform.launcher.listeners.SummaryGeneratingListener;
import org.junit.platform.launcher.listeners.TestExecutionSummary;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ReflectionUtils;
/**
* 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();
Thread.currentThread().setContextClassLoader(modifiedClassLoader);
try {
runTest(modifiedClassLoader, testClass.getName(), testMethod.getName());
runTest(extensionContext.getUniqueId());
}
finally {
Thread.currentThread().setContextClassLoader(originalClassLoader);
}
}
private void runTest(ClassLoader classLoader, String testClassName, String testMethodName) throws Throwable {
Class<?> testClass = classLoader.loadClass(testClassName);
Method testMethod = findMethod(testClass, testMethodName);
private void runTest(String testId) throws Throwable {
LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
.selectors(DiscoverySelectors.selectMethod(testClass, testMethod)).build();
.selectors(DiscoverySelectors.selectUniqueId(testId)).build();
Launcher launcher = LauncherFactory.create();
TestPlan testPlan = launcher.discover(request);
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 {
if (isModifiedClassPathClassLoader(extensionContext)) {
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
*/
class ModifiedClassPathExtensionOverridesParametrizedTests {
class ModifiedClassPathExtensionOverridesParameterizedTests {
@ParameterizedTest
@ForkedClassPath
Loading…
Cancel
Save