From ac6ad7c0f19007051df3432dec190542992c37e4 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 7 Nov 2022 13:34:54 +0000 Subject: [PATCH] Fix ModifiedClassPathExtension with parameterized tests Closes gh-33014 --- .../classpath/ModifiedClassPathExtension.java | 24 ++------- ...ssPathExtensionForkParameterizedTests.java | 52 +++++++++++++++++++ ...ExtensionOverridesParameterizedTests.java} | 2 +- 3 files changed, 56 insertions(+), 22 deletions(-) create mode 100644 spring-boot-project/spring-boot-tools/spring-boot-test-support/src/test/java/org/springframework/boot/testsupport/classpath/ModifiedClassPathExtensionForkParameterizedTests.java rename spring-boot-project/spring-boot-tools/spring-boot-test-support/src/test/java/org/springframework/boot/testsupport/classpath/{ModifiedClassPathExtensionOverridesParametrizedTests.java => ModifiedClassPathExtensionOverridesParameterizedTests.java} (97%) diff --git a/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/classpath/ModifiedClassPathExtension.java b/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/classpath/ModifiedClassPathExtension.java index 57ab4bbad3..ddac249c72 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/classpath/ModifiedClassPathExtension.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/classpath/ModifiedClassPathExtension.java @@ -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 invocation, ExtensionContext extensionContext) throws Throwable { if (isModifiedClassPathClassLoader(extensionContext)) { invocation.proceed(); diff --git a/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/test/java/org/springframework/boot/testsupport/classpath/ModifiedClassPathExtensionForkParameterizedTests.java b/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/test/java/org/springframework/boot/testsupport/classpath/ModifiedClassPathExtensionForkParameterizedTests.java new file mode 100644 index 0000000000..ebca9d6674 --- /dev/null +++ b/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/test/java/org/springframework/boot/testsupport/classpath/ModifiedClassPathExtensionForkParameterizedTests.java @@ -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 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); + } + +} diff --git a/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/test/java/org/springframework/boot/testsupport/classpath/ModifiedClassPathExtensionOverridesParametrizedTests.java b/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/test/java/org/springframework/boot/testsupport/classpath/ModifiedClassPathExtensionOverridesParameterizedTests.java similarity index 97% rename from spring-boot-project/spring-boot-tools/spring-boot-test-support/src/test/java/org/springframework/boot/testsupport/classpath/ModifiedClassPathExtensionOverridesParametrizedTests.java rename to spring-boot-project/spring-boot-tools/spring-boot-test-support/src/test/java/org/springframework/boot/testsupport/classpath/ModifiedClassPathExtensionOverridesParameterizedTests.java index 23214d8ec7..52bedbc88d 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/test/java/org/springframework/boot/testsupport/classpath/ModifiedClassPathExtensionOverridesParametrizedTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/test/java/org/springframework/boot/testsupport/classpath/ModifiedClassPathExtensionOverridesParameterizedTests.java @@ -32,7 +32,7 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Phillip Webb */ -class ModifiedClassPathExtensionOverridesParametrizedTests { +class ModifiedClassPathExtensionOverridesParameterizedTests { @ParameterizedTest @ForkedClassPath