Merge branch '6.2.x'

This commit is contained in:
Sam Brannen
2025-01-07 17:32:42 +02:00
5 changed files with 59 additions and 70 deletions

View File

@@ -44,24 +44,24 @@ import org.springframework.test.context.bean.override.BeanOverride;
* you can set the {@link #enforceOverride() enforceOverride} attribute to {@code true}
* — for example, {@code @TestBean(enforceOverride = true)}.
*
* <p>The instance is created from a zero-argument static factory method in the
* test class whose return type is compatible with the annotated field. In the
* case of a nested test class, the enclosing class hierarchy is also searched.
* Similarly, if the test class extends from a base class or implements any
* interfaces, the entire type hierarchy is searched. Alternatively, a factory
* method in an external class can be referenced via its fully-qualified method
* name following the syntax {@code <fully-qualified class name>#<method name>}
* &mdash; for example,
* <p>The instance is created from a zero-argument static factory method whose
* return type is compatible with the annotated field. The factory method can be
* declared directly in the class which declares the {@code @TestBean} field or
* within the type hierarchy above that class, including implemented interfaces.
* If the {@code @TestBean} field is declared in a nested test class, the enclosing
* class hierarchy is also searched. Alternatively, a factory method in an external
* class can be referenced via its fully-qualified method name following the syntax
* {@code <fully-qualified class name>#<method name>} &mdash; for example,
* {@code @TestBean(methodName = "org.example.TestUtils#createCustomerRepository")}.
*
* <p>The factory method is deduced as follows.
*
* <ul>
* <li>If the {@link #methodName()} is specified, look for a static method with
* that name.</li>
* <li>If a method name is not specified, look for exactly one static method
* named with either the name of the annotated field or the name of the bean
* (if specified).</li>
* <li>If the {@link #methodName methodName} is specified, Spring looks for a static
* method with that name.</li>
* <li>If a method name is not specified, Spring looks for exactly one static method
* whose name is either the name of the annotated field or the {@link #name() name}
* of the bean (if specified).</li>
* </ul>
*
* <p>Consider the following example.
@@ -146,15 +146,17 @@ public @interface TestBean {
/**
* Name of the static factory method that will be used to instantiate the bean
* to override.
* <p>A search will be performed to find the factory method in the test class,
* in one of its superclasses, or in any implemented interfaces. In the case
* of a nested test class, the enclosing class hierarchy will also be searched.
* <p>A search will be performed to find the factory method in the class in
* which the {@code @TestBean} field is declared, in one of its superclasses,
* or in any implemented interfaces. If the {@code @TestBean} field is declared
* in a nested test class, the enclosing class hierarchy will also be searched.
* <p>Alternatively, a factory method in an external class can be referenced
* via its fully-qualified method name following the syntax
* {@code <fully-qualified class name>#<method name>} &mdash; for example,
* {@code @TestBean(methodName = "org.example.TestUtils#createCustomerRepository")}.
* <p>If left unspecified, the name of the factory method will be detected
* based either on the name of the annotated field or the name of the bean.
* based either on the name of the {@code @TestBean} field or the {@link #name() name}
* of the bean.
*/
String methodName() default "";

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 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.
@@ -67,7 +67,7 @@ class TestBeanOverrideProcessor implements BeanOverrideProcessor {
Method factoryMethod;
if (!methodName.isBlank()) {
// If the user specified an explicit method name, search for that.
factoryMethod = findTestBeanFactoryMethod(testClass, field.getType(), methodName);
factoryMethod = findTestBeanFactoryMethod(field.getDeclaringClass(), field.getType(), methodName);
}
else {
// Otherwise, search for candidate factory methods whose names match either
@@ -78,7 +78,7 @@ class TestBeanOverrideProcessor implements BeanOverrideProcessor {
if (beanName != null) {
candidateMethodNames.add(beanName);
}
factoryMethod = findTestBeanFactoryMethod(testClass, field.getType(), candidateMethodNames);
factoryMethod = findTestBeanFactoryMethod(field.getDeclaringClass(), field.getType(), candidateMethodNames);
}
return new TestBeanOverrideHandler(