Support fully-qualified factory method names in @⁠TestBean

Prior to this commit, @⁠TestBean factory methods had to be defined in
the test class, one of its superclasses, or in an implemented
interface. However, users may wish to define common factory methods in
external classes that can be shared easily across multiple test classes
simply by referencing an external method via a fully-qualified method
name.

To address that, this commit introduces support for referencing a
@⁠TestBean factory method via its fully-qualified method name following
the syntax <fully-qualified class name>#<method name>.

Closes gh-33125
This commit is contained in:
Sam Brannen
2024-06-30 16:19:13 +02:00
parent c2f8d4803f
commit cc002875c4
5 changed files with 170 additions and 11 deletions

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;
/**
* Integration tests for {@link TestBean} that use bean factory methods defined
* in external classes.
*
* @author Sam Brannen
* @since 6.2
*/
@SpringJUnitConfig
class TestBeanForExternalFactoryMethodIntegrationTests {
@TestBean(methodName = "org.springframework.test.context.bean.override.example.TestBeanFactory#createTestMessage")
String message;
@Test
void test() {
assertThat(message).isEqualTo("test");
}
@Configuration
static class Config {
@Bean
String message() {
return "prod";
}
}
}

View File

@@ -24,6 +24,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.lang.NonNull;
import org.springframework.test.context.bean.override.example.ExampleService;
import org.springframework.test.context.bean.override.example.TestBeanFactory;
import org.springframework.util.ReflectionUtils;
import static org.assertj.core.api.Assertions.assertThat;
@@ -91,6 +92,64 @@ class TestBeanOverrideProcessorTests {
.withMessage("At least one candidate method name is required");
}
@Test
void findTestBeanFactoryMethodByFullyQualifiedName() {
Class<?> clazz = getClass();
Class<?> returnType = String.class;
String methodName = TestBeanFactory.class.getName() + "#createTestMessage";
Method method = this.processor.findTestBeanFactoryMethod(clazz, returnType, methodName);
assertThat(method).isEqualTo(ReflectionUtils.findMethod(TestBeanFactory.class, "createTestMessage"));
}
@Test
void findTestBeanFactoryMethodByFullyQualifiedNameWithNonexistentMethod() {
Class<?> clazz = getClass();
Class<?> returnType = String.class;
String factoryClassName = TestBeanFactory.class.getName();
String methodName = factoryClassName + "#bogus";
assertThatIllegalStateException()
.isThrownBy(() -> this.processor.findTestBeanFactoryMethod(clazz, returnType, methodName))
.withMessage("No static method found named %s in %s with return type %s",
"bogus", factoryClassName, returnType.getName());
}
@Test
void findTestBeanFactoryMethodByFullyQualifiedNameWithNonexistentClass() {
Class<?> clazz = getClass();
Class<?> returnType = String.class;
String methodName = "org.example.Bogus#createTestBean";
assertThatIllegalStateException()
.isThrownBy(() -> this.processor.findTestBeanFactoryMethod(clazz, returnType, methodName))
.withMessage("Failed to load class for fully-qualified method name: %s", methodName)
.withCauseInstanceOf(ClassNotFoundException.class);
}
@Test
void findTestBeanFactoryMethodByFullyQualifiedNameWithMissingMethodName() {
Class<?> clazz = getClass();
Class<?> returnType = String.class;
String methodName = TestBeanFactory.class.getName() + "#";
assertThatIllegalArgumentException()
.isThrownBy(() -> this.processor.findTestBeanFactoryMethod(clazz, returnType, methodName))
.withMessage("No method name present in fully-qualified method name: %s", methodName);
}
@Test
void findTestBeanFactoryMethodByFullyQualifiedNameWithMissingClassName() {
Class<?> clazz = getClass();
Class<?> returnType = String.class;
String methodName = "#createTestBean";
assertThatIllegalArgumentException()
.isThrownBy(() -> this.processor.findTestBeanFactoryMethod(clazz, returnType, methodName))
.withMessage("No class name present in fully-qualified method name: %s", methodName);
}
@Test
void createMetaDataForUnknownExplicitMethod() throws Exception {
Class<?> clazz = ExplicitMethodNameTestCase.class;
@@ -176,7 +235,6 @@ class TestBeanOverrideProcessorTests {
static class BaseTestCase {
@TestBean(methodName = "factory")
public String field;
static String factory() {