Support @⁠TestBean factory methods defined in interfaces

Prior to this commit, @⁠TestBean factory methods were required to be
defined in the test class or one of its superclasses. However, users
may wish to define common factory methods in interfaces that can be
shared easily across multiple test classes simply by implementing the
necessary interface(s).

This commit therefore switches from ReflectionUtils to
MethodIntrospector to find @⁠TestBean factory methods in implemented
interfaces within the type hierarchy of the test class.

Closes gh-32943
This commit is contained in:
Sam Brannen 2024-06-03 16:04:05 +02:00
parent 6a761412a8
commit d185bb1d97
3 changed files with 95 additions and 10 deletions

View File

@ -28,6 +28,7 @@ import java.util.Objects;
import java.util.Set; import java.util.Set;
import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.core.MethodIntrospector;
import org.springframework.core.ResolvableType; import org.springframework.core.ResolvableType;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.test.context.TestContextAnnotationUtils; import org.springframework.test.context.TestContextAnnotationUtils;
@ -88,24 +89,23 @@ class TestBeanOverrideProcessor implements BeanOverrideProcessor {
supportedNames.contains(method.getName()) && supportedNames.contains(method.getName()) &&
methodReturnType.isAssignableFrom(method.getReturnType())); methodReturnType.isAssignableFrom(method.getReturnType()));
List<Method> methods = findMethods(clazz, methodFilter); Set<Method> methods = findMethods(clazz, methodFilter);
if (methods.isEmpty() && TestContextAnnotationUtils.searchEnclosingClass(clazz)) { if (methods.isEmpty() && TestContextAnnotationUtils.searchEnclosingClass(clazz)) {
methods = findMethods(clazz.getEnclosingClass(), methodFilter); methods = findMethods(clazz.getEnclosingClass(), methodFilter);
} }
Assert.state(!methods.isEmpty(), () -> """ int methodCount = methods.size();
Assert.state(methodCount > 0, () -> """
Failed to find a static test bean factory method in %s with return type %s \ Failed to find a static test bean factory method in %s with return type %s \
whose name matches one of the supported candidates %s""".formatted( whose name matches one of the supported candidates %s""".formatted(
clazz.getName(), methodReturnType.getName(), supportedNames)); clazz.getName(), methodReturnType.getName(), supportedNames));
long nameCount = methods.stream().map(Method::getName).distinct().count(); Assert.state(methodCount == 1, () -> """
int methodCount = methods.size();
Assert.state(nameCount == 1, () -> """
Found %d competing static test bean factory methods in %s with return type %s \ Found %d competing static test bean factory methods in %s with return type %s \
whose name matches one of the supported candidates %s""".formatted( whose name matches one of the supported candidates %s""".formatted(
methodCount, clazz.getName(), methodReturnType.getName(), supportedNames)); methodCount, clazz.getName(), methodReturnType.getName(), supportedNames));
return methods.get(0); return methods.iterator().next();
} }
@Override @Override
@ -138,10 +138,8 @@ class TestBeanOverrideProcessor implements BeanOverrideProcessor {
} }
private static List<Method> findMethods(Class<?> clazz, MethodFilter methodFilter) { private static Set<Method> findMethods(Class<?> clazz, MethodFilter methodFilter) {
List<Method> methods = new ArrayList<>(); return MethodIntrospector.selectMethods(clazz, methodFilter);
ReflectionUtils.doWithMethods(clazz, methods::add, methodFilter);
return methods;
} }

View File

@ -0,0 +1,31 @@
/*
* Copyright 2002-2024 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.test.context.bean.override.convention;
/**
* Shared {@link TestBean @TestBean} factory methods.
*
* @author Sam Brannen
* @since 6.2
*/
interface TestBeanFactory {
public static String createTestMessage() {
return "test";
}
}

View File

@ -0,0 +1,56 @@
/*
* Copyright 2002-2024 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.test.context.bean.override.convention;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
/**
* {@link TestBean @TestBean} integration tests for test bean factory methods
* defined in implemented interfaces.
*
* @author Sam Brannen
* @since 6.2
*/
@SpringJUnitConfig
public class TestBeanInterfaceIntegrationTests implements TestBeanFactory {
@TestBean(methodName = "createTestMessage")
String message;
@Test
void test() {
assertThat(message).isEqualTo("test");
}
@Configuration
static class Config {
@Bean
String message() {
return "prod";
}
}
}